Advertisement
Guest User

Untitled

a guest
May 5th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. // File: includes.php
  2.  
  3. <?php
  4.  
  5.     // Login Script
  6.    
  7.     if(isset($_POST['login'])){
  8.         // Grab the username based on the form entry...
  9.  
  10.         $usr_name = mysql_real_escape_string(addslashes($_POST['usr_name']));
  11.         $usr_pass = mysql_real_escape_string(addslashes($_POST['usr_pass']));
  12.        
  13.         // Compare entries based on SQL or RAW variables.
  14.         // For the sake of this example, we will use RAW variables... but you will want to use an SQL table for dynamic users.
  15.        
  16.         $username = 'Foo';
  17.         $password = 'Bar';
  18.         $userid = '1'; // Would be grabbed from a table.
  19.  
  20.         if($usr_name != $username){
  21.             echo 'There was a problem with the username or password you entered.  Please go back and try again.';
  22.         }
  23.         elseif($usr_password != $password){
  24.             echo 'There was a problem with the username or password you entered. Please go back and try again.';
  25.         }
  26.         else{
  27.             // Preform login...
  28.  
  29.             setcookie('user',$userid,time()+3600);
  30.             echo 'Congratulations, your login was successful.';
  31.         }
  32.     }
  33.  
  34.     // End Login Script
  35.  
  36.  
  37. ?>
  38. // FIle: logout.php
  39.  
  40. <?php
  41.     // Logout Script...
  42.     if(isset($_GET['usr']){
  43.         setcookie('user',$_GET['user'],time()-5200);
  44.     }
  45.     else{
  46.         echo 'No valid cookie id... you fail at php nub.';
  47.     }
  48. ?>
  49.  
  50.  
  51. // File: index.php
  52.  
  53. <php
  54.  
  55. require('includes.php');
  56.  
  57.     // Check to see if user is logged in...
  58.  
  59.     if(isset($_COOKIE['user'])){
  60.         // Extra check for security... Compare with the DB / Raw variable.
  61.         // Normally you would do an SQL statement to see if the Id exists, but in our example we will just draw the raw variable from the includes.php.
  62.  
  63.         $realid = '1'; // Again would be drawn from a DB query.
  64.  
  65.         if($realid != $userid){
  66.             echo 'Your cookie was falsely set, please <a href="logout.php?usr=' . $userid . '">Logout</a> and attempt to login again.';
  67.         }
  68.         else{
  69.             echo 'Main page, logged in... hello world!';
  70.  
  71.             //Here is where you would put your main page's content... whatever that might be.
  72.         }
  73.     }
  74.     else{
  75.         //Not logged in, ergo print the login form.
  76.  
  77.         echo '<form action="" method="post">',
  78.             'Username: <input type="text" name="usr_name" /><br />',
  79.             'Password: <input type="password" name="usr_password" /><br />',
  80.             '<input type="submit" name="login" value="Process Login" />';
  81.     }
  82.  
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement