Jump to content
Sign in to follow this  
Neo

PHP Session - more examples would be nice

Recommended Posts

Hi

 

I would like to request an expansion to PHP Sessions as I think that the provided example of how to use sessions for making a page counter is too simple.

 

Therefore I would like to suggest that a small tutorial about how-to-make-a login system using session to be added to the PHP Session chapter.

Share this post


Link to post
Share on other sites

index.php

<?phpsession_start();if (isset($_SESSION['username'])) {//User has logged inecho "Logged in as: " . $_SESSION['username'];} else {echo "<h3>Login to have access on page.</h3><form method='post' action='login.php'>Username: <input type='text' name='user'>Password: <input type='password' name='pass'><input type='submit' value='Login'></form>";}?>

login.php

<?phpsession_start();$user = "John"; //Example username$pass = "125Eas%"; //Should do this with SQL but easier to understand with phpif (!isset($_SESSION['username'])) {if (isset($_POST['user']) && $_POST['user'] == $user && isset($_POST['pass']) && $_POST['pass'] == $pass) {$_SESSION['username'] = $_POST['user'];echo "<p>Login success. You are logged in as: " . $_SESSION['username'] . "</p>Return to mainpage, click <a href='index.php'>here</a>!";} else {echo "<p>Wrong username or password.</p>";}} else {echo "Already logged in as: " . $_SESSION['username'];}?>
Edited by Mudsaf

Share this post


Link to post
Share on other sites

This works. Of course you can immediately begin to consider various refinements. For example raw input from a user can't be trusted because it could be XSS or an attempt at SQL injection. Also don't you want to limit them to only so many failed login attempts per hour or per day? Also they may need a way to recover if they really forgot their username or password. Also the password and username are flowing across the internet in plain text.

 

To get a giant headache you can study...

 

https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet

Edited by davej

Share this post


Link to post
Share on other sites

Typically sites like this focus on offering references or basic examples of the common core and native features of a language. Something like a login system is typically the product of putting together many individual pieces, usually encompassing multiple languagues, into one larger, high-level system and typically fall out of scope for a site like this. Given that, there are numerous tutorials an examples online of many common website features, like login / registration systems, file uploaders, etc.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...