Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,226 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,037 people online right now. Registration is fast and FREE... Join Now!





Login using Sessions in PHP

This is some code for "securing" your pages using Sessions in PHP. By securing I mean pages that can only viewed by a logged in user This "snippet" actually consists of 3 snippets, first is what to put in login_check.php, second is what to put in login.php, third is what to put in each page in your site that must be secured (only a logged in user can view)

Submitted By: PsychoCoder
Actions:
Rating:
Views: 2,949

Language: PHP

Last Modified: February 18, 2008
Instructions: 1) Add the first snippet to a page named login_check.php

2) Add the 2nd snippet to a page named login.php

3) Add the 3rd snippet to any page that needs to be secured

4) Make sure you login form's form tag looks like this


You can change the database query, this is just the way I chose to do it

Snippet


  1. /* Snippet #1 */
  2. <?php
  3. // login_check.php
  4. define("server", "your_server");
  5. define("user", "your_name");
  6. define("password", "your_pass");
  7. define("name", "your_dbname");
  8.  
  9. var $connection;
  10. $this->connection = mysql_connect(server,user,pass) or die(mysql_error());
  11. mysql_select_db(name, $this->connection) or die(mysql_error());
  12.  
  13. function is_logged_in () {
  14.   if (!($_SESSION["id"]) || ($_SESSION["id"] == "") || ($_SESSION["id"] == 0)) {
  15.     Header("Location: ./login.php");
  16.     exit();
  17.   }
  18. }
  19.  
  20. function clean_input($input) {
  21.  
  22.   $clean = array("\\",'<','>','`',':',';','/','(',')','{','}','[',']');
  23.   //$with = array();
  24.   return str_ireplace($clean,'', $input);
  25. }
  26.  
  27. function login_check ($forms) {
  28.   $error = "";
  29.   $username = clean_input($forms["username"]);
  30.   $password = clean_input($forms["password"]);
  31.   if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
  32.   if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
  33.   /* from here, do your sql query to query the database to search for existing record with correct username and password */
  34.   $query = "SELECT password, username FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
  35.   $result = mysql_query($query, $this->connection);
  36.   if(!$result || (mysql_numrows($result) < 1)) {
  37.      $error = "Invalid username or password";
  38.   }else
  39.       {
  40.         $error = "";
  41.       }
  42.   if (trim($error)!="") return $error;
  43. }
  44.  
  45. function login ($forms) {
  46.   $username = clean_input($forms["username"]);
  47.   $password = clean_input($forms["password"]);
  48.   /* do your sql query again, but now returning the id of member */
  49.   $query = "SELECT member_id FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
  50.   $result = mysql_query($query, $this->connection);
  51.   $result = mysql_query($query, $this->connection);
  52.   if(!$result || (mysql_numrows($result) < 1)) {
  53.      $id = 0;
  54.   }else
  55.       {
  56.         $id = $result;
  57.       }
  58.   return $id;
  59. }
  60. ?>
  61.  
  62. /* Snippet #2 */
  63. <?php
  64. // login.php
  65. include ("login_check.php");
  66. if ($_POST) {
  67.   $error = login_check($_POST);
  68.   if (trim($error)=="") {
  69.     $_SESSION["id"] = login($_POST);
  70.     Header("Location: ./index.php") /* Redirect validated member */
  71.     exit();
  72.   } else {
  73.     print "Error:$error";
  74.   }
  75. }
  76. ?>
  77.  
  78. /* Snippet #3 */
  79. <?php
  80.   // index.php
  81.   include("login_check.php");
  82.   is_logged_in();
  83. ?>

Copy & Paste


Comments


no2pencil 2008-02-18 13:24:38

Sessions is one of the harder concepts to server side programming. Thank you for your snippet, I'm sure a lot of readers will find this information useful!

realwish 2008-11-18 14:15:56

thanks a lot


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month