Question

Good day.

I have questions about the login system , that disturbed me quite a long time. For this i want you to imagine that i have 2 pages login.php and userpage.php. The login page contains fields for input of user name and password. While userpage contains all the information about the logined user. When user inputs his data, some class Connection checks him in the database and if user exists, creates a session.

  1. When I'm creating a redirection from login.php to userpage.php, how should i redirect users data? (Should I use global arrays (like $_SESSION) to transfer the info or I should connect the db again from the user page?)

  2. Should I create some multi-threading (Do not judge strictly, I'm a newbie) for userpage.php, to be created for multiple users, which are trying to login at the same time?

  3. How should I protect the information (code side), for being hard to read? (For example Facebook pages source-code. because i don't want some "bad guys" to view my sources) and other things.

  4. How can I make some users to see what the others can't ? For example userpage.php shows different links and information for different users and all the information for me .

  5. How can i prevent membership.php from being viewed?(Is there some other way than using header?)

  6. How can i prevent my require and require_once from being viewd at the login.php and userpage.php ?

Was it helpful?

Solution

1.) When I'm creating a redirection from login.php to userpage.php, how should i redirect users data? (Should I use global arrays (like $_SESSION) to transfer the info or I should connect the db again from the user page?)

You need to have a connection to the db everytime you want to get the user's data. You can create a session to store a unique attribute for the user, like $_SESSION['id'], when the user is successfully logged in, and you can use that value on any page to query the db and get the necessary user data.

2.) Should I create some multi-threading (Do not judge strictly, I'm a newbie) for userpage.php, to be created for multiple users, which are trying to login at the same time?

No, you don't need to worry about users connecting at the same time. The server can handle this. When you have a million users or so, you can start considering this. (Although, even then I'm not too sure. Unfortunately I've never had that problem ;) )

3.) How should I protect the information (code side), for being hard to read? (For example Facebook pages source-code. because i don't want some "bad guys" to view my sources) and other things.

You cannot prevent anyone from seeing your markup and styles, that is, your html and css, or any client side scripting, like javascript. However, your php is server side and not displayed in the source. The 'bad guys' will not be able to view source to see your db connections, php logic, etc.

4.) How can I make some users to see what the others can't ? For example userpage.php shows different links and information for different users and all the information for me .

There are different approaches to take. The simplest is probably to store the user's 'permission level' in the db, and then check that every time you load content. For example,

    if ($user['permission']==1)
        // Show something
    elseif ($user['permission']==2)
        // show something else

5.) How can i prevent membership.php from being viewed?(Is there some other way than using header?)

The easiest way to do this is by checking to see if there is an active session, and if not, redirect:

    if (!isset($_SESSION['id']))
        header("Location: login.php");

6.) How can i prevent my require and require_once from being viewed at the login.php and userpage.php ?

Not too sure what you mean by this, but consider this: require and require_once are the exact same as including the code directly in the file. If you are referring to them being viewed directly by the client by hitting 'view source', don't worry - see answer to question 3.

Note:

These answers are simplified, and there are plenty of other complications to consider. Some of this stuff may not make sense, but I wouldn't sweat it too much. I would recommend starting small - find a decent tutorial or two on how to create a simple user database, a registration, and login page, and start there. No answers you get here will substitute research, practice, and trial and error. Start small, and things will quickly become clearer as you progress.

OTHER TIPS

  1. Save the users state in a cookie or in a session. Note that you need the session_start() the userpage.php page as well as the rest of the page were the user is connected. More info on http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL
  2. See the above link.
  3. No one can read PHP code because it is server side and not client side. So your code is secure already from its own structure.
  4. Let users have different level from the swl-database. If a user got auth 1 they see some links, if they got user auth 2 they see other things.
  5. See page from answer 1
  6. See page from answer 1

Considering your stated fact that you are newbie,I will also assume that the login system is more of practice thing and not a real world app.

Now to answer your queries point-wise.

  1. Storing data in SESSION variables is alright.However,do not store too many data in SESSIONS.I would suggest just store the userid for the user and use that to gather and display info in the userpage.php. As the app gets bigger,you will definitely need to make connections in each individual page.
  2. Use SESSION and COOKIE combination to create multiple user logins. However,Refrain from trying to implement/allow same browser multiple logging-in.SECURITY ISSUE.
  3. PHP source code is anyways not readable from client-side.Regarding javascript & css-u can maybe minify it.But that would still not make it client-safe.
  4. There are many ways to implement this.Maybe have a $_SESSION['admin'] =true when a admin logs-in and use it to display/hide info on userpage.php.
  5. Same as NEXT
  6. What it is that u want to hide?If its HTML/JS ,u dont't have much choice. One solution may be to use if-else in ur php code and restrict display of code present in header.php and the pages included via require and require_once.

This is a very basic guide.Your strategies may vary depending on the complexity of your application and also if/when you start using framweorks . Happy logging-in !!

ADDITIONS wrt to application structure. Considering that your end product would be a system that allows a user to register and login/logout,i would suggest a following structure to begin with.

Structure-

index.php
    |--action    
         |---register.php
         |---logged_in_user_landing.php
  1. index.php-- This is main page and used to redirect to individual pages based on actions. check if SESSION is set.

    If yes,include action/logged_in_user_landing.php else include action/register.php.

    As actions increase,you can add if-else and include more pages accordingly.

  2. In register.php,u have the form for login. On submit, redirect to index.php (via form action).

  3. establish db connection in index page and check username-password combination.If correct,set the SESSION for that user and include the 'action/logged_in_user_landing.php'.

  4. Have a unique identifier sent along when redirecting from each individual page,So that u can identify what to do in index.php.

This is a very simple architecture that should get u started.Its kind of a controller based architecture and will help you in the future when u go into MVC architectures.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top