Hey I was wanting to make a login form on my site where the people could come and login in and it would then direct them to a page that has personalized stuff for them, like there name, and links to stuff for them. Also I would like it to display and error on the page that the login form is on if they type in the wrong username or password or if the username they typed in doesn't exist. So I got on google and searched for PHP login scripts and this is one of the results I got...

http://php.about.com/od/finishedphp1/ss/php_login_code.htm

I wouldn't need the registration page, but are the other scipts good?

    Did u try it, does it suits your needs?

      hi

      This script is as http://php.about.com/

      There are also other good TUTORIALS at http://php.about.com/
      for example

      Articles & Resources

      Using the PHP MAIL function
      Using the PHP MAIL function to send form data to email
      Using Cookies with PHP
      Cookies are used to store information about a user on their computer.
      Basic PHP Sessions
      A tutorial explaining the basics of using sessions in PHP
      Execute PHP from a .html File
      How to execute PHP code in a file with a .html extension
      Introduction to Preg in PHP
      Understanding the preg_grep, preg_match, preg_match_all, preg_replace, and preg_split functions and how to use them on your PHP website

      PHP Login Script
      http://php.about.com/od/finishedphp1/ss/php_login_code.htm
      Loooks as a good basic script for login, with Cookies and MySQL database.

      Normally we would store user email and maybe IP-address, too,
      but this script uses only 3 fields: ID, username, password

      CREATE TABLE users (
      ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
      username VARCHAR(60), 
      password VARCHAR(60)
      )

      Here is how test is done at page, if user is logged in or not

      <?php
      // Connects to your Database
      mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
      mysql_select_db("Database_Name") or die(mysql_error());
      
      //checks cookies to make sure they are logged in
      if(isset($_COOKIE['ID_my_site']))
      {
      $username = $_COOKIE['ID_my_site'];
      $pass = $_COOKIE['Key_my_site'];
      $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
      while($info = mysql_fetch_array( $check ))
      {
      
      //if the cookie has the wrong password, they are taken to the login page
      if ($pass != $info['password'])
      { header("Location: login.php");
      }
      
      //otherwise they are shown the admin area
      else
      {
      echo "Admin Area<p>";
      echo "Your Content<p>";
      echo "<a href=logout.php>Logout</a>";
      }
      }
      }
      else
      
      //if the cookie does not exist, they are taken to the login screen
      {
      header("Location: login.php");
      }
      ?>

        Thankyou for your reply Halojoy, but what do you mean by this:

        Here is how test is done at page, if user is logged in or not

          Here is how test is done at page, if user is logged in or not

          I this script that part is called 'Member Area'
          but of course the same code should be used to PROTECT any page
          where a user needs to be logged in to access.

          What that part does is:
          1. test if cookie and user is logged in
          2. if not logged in -> redirect to login.php
          3. if okay logged in ->show rest of page

          This code checks our cookies to make sure the user is logged in,
          the same way the login page did.
          If they are logged in, they are shown the members area.
          If they are not logged in they are redirected to the login page.

          <?php
          // Connects to your Database
          mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
          mysql_select_db("Database_Name") or die(mysql_error());
          
          //checks cookies to make sure they are logged in
          if(!isset($_COOKIE['ID_my_site']))
          {   //if the cookie does not exist, they are taken to login
              header("Location: login.php");
              exit;
          }
          else
          {   // Check the cookie
              $username = $_COOKIE['ID_my_site'];
              $pass = $_COOKIE['Key_my_site'];
              $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
              while($info = mysql_fetch_array( $check ))
              {
                  //if the cookie has wrong password, they are taken to login
                  if ($pass != $info['password'])
                  {
                      header("Location: login.php");
                      exit;
                  }
              }
          }
          
          // rest of this page
          // ONLY for logged in user
          
          ?>

          This login check can be saved in a separate include page: check_logged.php
          and to make a check in beginning of any page, we only need to add this

          <?php
          include ( "check_logged.php" );
          
          // rest of page
          // ONLY for Logged in users
          
          ?>

            Oh wow thankyou, that made it really easy to understand. So I would put the HTML for the members page below this?

            <?php 
            include ( "check_logged.php" ); 
            
            // rest of page 
            // ONLY for Logged in users 
            
            ?>
              IlikeTheWeb wrote:

              Oh wow thankyou, that made it really easy to understand. So I would put the HTML for the members page below this?

              <?php 
              include ( "check_logged.php" ); 
              
              // rest of page 
              // ONLY for Logged in users 
              
              ?>

              yes,
              can be plain HTML or PHP code or a combination of BOTH.

              But in order to work, page extension have to be: .php
              This is because that first include line is in PHP

              Example:

              <?php include ( "check_logged.php" ); ?>
              <html>
              <head>
              <title>protected test page</title>
              </head>
              <body>
              Hello and welcome to my test page<br>
              If you can read this<br>
              you are logged in!
              </body></html>

                Awesome, thankyou so much, you've helped me out alot. Also I was wondering how it displays the errors, like if they type in a wrong username or password. How do I tell it to put the errors in a certain place on the page?

                  Write a Reply...