A Login Script


[--Login Script--]
------------------
[--Level = Medium--]
------------------

Welcome to another one of my tutorials on scripting. This session is continuing from the registration script and you will
not understand most of the coding + im using most of the same database rows. First make a script called login.php and add this to it. This will be the login box.

PHP Code
  1. <html>
  2. <head><title>LOGIN</title></head>
  3. <body bgcolor=#333333>
  4. <form method="POST" action="logincheck.php">
  5. <table width=50% bgcolor=#666666 border=1 bordercolor=black cellpadding=0 cellspacing=0 frame=box rules=all>
  6. <tr><td>
  7. <font color=red><div align=center>USERNAME: <input style="background-color:#222222; font: 10pt verdana; color:#ffffff; border: 1px solid #555555;" type=text id="username" name="username" size="24">
  8. <br>
  9. PASSWORD: <input type="password" style="background-color:#222222; font: 10pt verdana; color:#ffffff; border: 1px solid #555555;" id=password name="password" size="24">
  10. <br>
  11. <input style="background-color:#CD8500; color:#000000; border: 1px solid #555555;" type="submit" name="Submit" value="Login"></div>
  12. <br>
  13. <a href=register.php>Register</a>
  14. </td></tr>
  15. <td>
  16. <div align="center">
  17. NEW
  18. <br>
  19. Welcome To Your Site!
  20. </div>
  21. </td>
  22. </table>
  23. </form>
  24. </body>
  25. </html>


Now add this to the script, this is the php area of the code at the top.

PHP Code
  1. <?php
  2. include_once"includes/db_connect.php";
  3. $ud=mysql_num_rows(mysql_query("SELECT * FROM users"));
  4. ?>


Now what you must do is add a row to your table `users` in your database called `ban`, make sure
it is 'enum' with the values '0','1' . Also add another row called 'online' and just make it
varchar100, no default value.

Now make another script named logincheck.php and insert this inside it.

PHP Code
  1. <?
  2.  
  3. session_start();
  4.  
  5. header("Cache-control: private");
  6.  
  7. include 'includes/db_connect.php';
  8.  
  9.  
  10.  
  11. if ($_SESSION['username']){
  12.  
  13. echo "
  14.  
  15. <SCRIPT LANGUAGE='JavaScript'>
  16.  
  17. window.location='logged_in.php'; /// When they are logged in already redirect them here.
  18.  
  19.  
  20.  
  21. </script>
  22.  
  23. ";
  24.  
  25. exit();
  26.  
  27. }
  28.  
  29.  
  30. if (!$_SESSION['username'] || !$_SESSION['email']){
  31.  
  32. $username = $_POST['username']; /// What the user put in the username box.
  33.  
  34. $password = $_POST['password']; /// What the user put in the password box.
  35.  
  36. $username = strip_tags($username);
  37.  
  38. $password = strip_tags($password);
  39.  
  40. $ip = $REMOTE_ADDR; /// The users IP address
  41.  
  42. $domain = $_SERVER['REMOTE_ADDR'];
  43.  
  44.  
  45.  
  46. $username=strtolower($username); /// Put the username in lower case letters.
  47.  
  48.  
  49.  
  50. if((!$username) || (!$password)){
  51.  
  52. echo "Please Enter All Necessary Fields."; /// If the username or
  53. password are empty then echo this.
  54.  
  55. }else{
  56.  
  57.  
  58. ///check INFO
  59.  
  60. $sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
  61.  
  62. /// This line above checks if there is anyone in the database with that username and password.
  63.  
  64. $login_check = mysql_num_rows($sql);
  65.  
  66. /// This checks how many people are on that database.
  67.  
  68. $ban = mysql_fetch_object($sql);
  69.  
  70. if ($ban->banned = "1"){
  71. echo "This user has been banned, please contact admin via email to find out why.";
  72. exit();
  73.  
  74. /// If the user is banned echo saying that he is banned and exit the session.
  75.  
  76. }else{
  77.  
  78. ///other
  79.  
  80. if ($login_check > '0'){
  81.  
  82.  
  83.  
  84. ini_set(session.cookie_lifetime, "3600");
  85.  
  86. session_register('username');
  87.  
  88. $_SESSION['username'] = $username;
  89.  
  90. session_register('email_address');
  91.  
  92. $_SESSION['email_address'] = $email_address;
  93.  
  94. /// security measures
  95.  
  96.  
  97. $timestamp = time();
  98.  
  99. $timeout = $timestamp-$timeoutseconds;
  100.  
  101. $cool = gmdate('Y-m-d h:i:s');
  102.  
  103. mysql_query("UPDATE users SET online='$timestamp' WHERE username='$username'");
  104.  
  105.  
  106.  
  107. mysql_query("UPDATE users SET ip='$domain' WHERE username='$username'");
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. ?>
  116.  
  117. <meta http-equiv="Refresh" content=0;url="logged_in.php"> /// redirect them to the logged in page.
  118.  
  119. <?
  120.  
  121.  
  122.  
  123. } else {
  124.  
  125. echo "Please Make Sure You Have Entered To Right Password/Username Combination. If You Have, Your Account May Not Be Activated
  126.  
  127. ";
  128.  
  129. include 'login.php'; /// If their details are wrong echo the above and below that show login.php
  130.  
  131. }}}
  132.  
  133. ?>
ilyas-shezad's Avatar
Views:
3,650
Rating:
Posted on Saturday 8th December 2007 at 11:22 AM
Dean
Dean's Avatar
wheres th sql info?
Posted on Sunday 19th August 2007 at 11:47 AM
ilyas-shezad
ilyas-shezad's Avatar
does it? i wasnt sure + this is new 1 :)
Posted on Saturday 18th August 2007 at 03:39 PM
-=InSaNe=-
-=InSaNe=-'s Avatar
Uhh.. The RMB usersystem allready has 'Login.php' so why make it again?