Results 1 to 27 of 27

Thread: Starting off with login/registration

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Question Starting off with login/registration

    I have made a very basic registration page whereby hitting 'submit' will add the details into my Users table with no validation or anything.

    What I want to do now is start setting the login/registration side of my site up so just a few questions I hope you can answer:

    1. When I fill in my registration form I would like to perform validation and then authentication. I understand that I can pass the form values to authentication.php (or whichever file) and can do the checks there however is there a way I can do the validation first to make sure the form is filled in corectly AND maybe display a line on that page. Eg. if username is not filled in, it could just say 'please fill in a username' -- I'm thinking of using the 'or die' function and just making the user press back to refill the form, but is there a better way??

    [I have a few more but I guess I should ask them when this one is done]

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Starting off with login/registration

    I would always argue that the best way of validating forms server side is to split the script in two.

    logon_display.php - A template that displays the form.
    logon_input.php - The script that receives the input, validates and verifys the logon credentials.

    Doing it like this will enable you to keep the form and the script that actually logs the user on separate entities.

    login_display.php may look something like this:
    PHP Code:
    <?php
        
    /* disable notices as variables will be undefined on first display */
        
    error_reporting(E_ALL E_NOTICE);
        
    session_start();

        
    $formMessage $_SESSION['formMessage'];
        
    $username $_SESSION['username'];
        
    $password $_SESSION['password'];
    ?>
    <html>
        <head>
            <title>Logon</title>
        </head>
        <body>
            <p>Enter your login details below:</p>
        <form action="logon_input.php" method="post">
            <div><?php echo($formMessage?></div>
            <div>
            <p><?php echo($username['msg']) ?></p>
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" 
                    value="<?php echo($username['value']) ?>" />
            </div>
            <div>
            <p><?php echo($password['msg']) ?></p>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" />
            </div>
            <div><input type="submit" /></div>
            </form>
        </body>
    </html>
    Your logon_input.php will process the form and validate it. If validation and authentication success, a sesion variable called authenticated is set to true and the user is redirected to the next page. If it fails, the appropriate messages are set and added to the session and the user is redirected back to the logon page.
    PHP Code:
    <?php
        session_start
    ();
        
    $_SESSION = array(); // clear any old session data

        /* check a username is present */
        
    if ((! isset($_POST['username'])) || trim($_POST['username'] == '')) {
            
    $_SESSION['formMessage'] = 'Error Processing Request';
            
    $_SESSION['username']['msg'] = 'Username Cannot be Left Blank';
            
    header('Location: logon_display.php');
        exit;
        }

        
    /* check a password is present */
        
    if ((! isset($_POST['password'])) || trim($_POST['password'] == '')) {
            
    $_SESSION['formMessage'] = 'Error Processing Request';
            
    $_SESSION['password']['msg'] = 'Password Cannot be Left Blank';
            
    header('Location: logon_display.php');
        exit;
        }    

        
    $username $_POST['username'];
        
    $password $_POST['password'];

        if (
    authenticate($username$password)) {
        
    /* valid username and password */
        
    $_SESSION['authenticated'] = true;
        
    header('Location: next_page.php');
        } else {
        
    $_SESSION['formMessage'] = 'Invalid Username or Password';
        
    $_SESSION['user']['value'] = $username;
        
    header('Location: logon_display.php');
        }
        
        
    /* this function will authenticate the user, prehaps via a database
           or a file. in this case i just put the username and password in an
           array */
        
    function authenticate($username$password)
        {
        
    $users = array ('user1' => 'password1',
                        
    'user2' => 'password2');
                
        
    /* it is cruciual that you uncomment these lines if you a inserting
           these values into a query to prevent sql injection. These are for
           Mysql, so you my have to modify them appropriately for other
           databases. */
        // $username = mysql_escape_string($username);
        // $password = mysql_escape_string($password);
        
        
    return array_key_exists($username$users) && ($users[$username] == $password);
        }
    ?>
    Notice how the session is used, once logged on to store a variable to indicate the user has logged on. You should check this exists on every page that requires authentication and to log the user out you simply set this variable to false.

    next_page.php contains an include to a small script called auth.php that checks for authentication. To enable authentication for a specified page, simply put this line at the top of the script:
    PHP Code:
    require 'auth.php'
    auth.php
    PHP Code:
    <?php
        session_start
    ();
     
        if (! @
    $_SESSION['authenticated']) {
        
    header('Location: logon_display.php');
        exit;
        }
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Starting off with login/registration

    You can see a working example of all three scripts here:

    http://adam.codedv.com/examples/logon/logon_display.php
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Cheers! Thanks for the links, I broke it down into 2 sections as you guys explained:

    Javascript to check on a page if stuff has been filled out right.

    And then checking things with PHP code to make sure again!

    Thx, I used this link as well: http://www.php-mysql-tutorial.com/fo...n-with-php.php

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    I might as well continue with this thread (not sure if I should but I'll do that as the 2nd question is related)...


    This is how I've been told to deal with authentication. It's quite simple I guess but it works fine for the stage I'm working at:

    1 - I've made a hash field in my users table.
    2 - When you register it is set to 0.
    3 - When you login successfully the hash is set to a random number.
    4 - I put the hash into a cookie as well.
    5 - Each time a user logs in the hash is randomly generated.

    6 - If I want to check if a user has logged in I just compare hash in cookie and hash field.

    This is what I've got so far:

    Code:
    //Checks if there is a login cookie
    if(isset($_COOKIE['my_hash']))
    //if there is, it checks to see if my_hash = userhash
    { 
    $c_user = $_COOKIE['my_username']; 
    $c_hash = $_COOKIE['my_hash'];
            
    $check = mysql_query("SELECT * FROM Users WHERE Username = '$c_user'")or die(mysql_error());
     
    while($info = mysql_fetch_array( $check ))      
    {
    if ($c_hash != $info['Userhash']) // not logged in
    {
     
    }
    else // logged in already
    {
     
    header("Location: users.php");
    }
    }
    }

    What I want to do is put that into a function so that I can call it whenever I need to check if the user is logged in or not. I guess because I am included username with it I can get the users details...

    How should I put it into a function, and how would I correctly call it?? Thx

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Starting off with login/registration

    Quote Originally Posted by visualAd
    PHP Code:
    <?php
        
    /* disable notices as variables will be undefined on first display */
        
    error_reporting(E_ALL E_NOTICE);
    ?>
    Ouch, not very cool, IMO. Shutting off notices because some variables might not yet be defined works for those variables, but it also works for ALL variables, which means if you screw up somewhere on the page...you lose a quick reference to what the problem might be.

    I think it'd be wiser to do something like this to take care of those variables:

    PHP Code:
    $formMessage = isset( $_SESSION['formMessage'] ) ? 
        
    $_SESSION['formMessage'] : "";
    $username = isset( $_SESSION['username'] ) ? 
        
    $_SESSION['username'] : "";
    $password = isset( $_SESSION['password'] ) ? 
        
    $_SESSION['password'] : ""
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Starting off with login/registration

    I would only ever do that in a template script like the one above. I am usedto it because I use Smarty . But the best way is to check if a variable is defined before using it and define all variables that may be set.
    PHP Code:
    <?php
        session_start
    ();

        
    $formMessage = isset($_SESSION['formMessage'])?$_SESSION['formMessage']:'';
        
    $username = isset($_SESSION['username'])?$_SESSION['username']:'';
        
    $password = isset($_SESSION['password'])?$_SESSION['password']:'';
    ?>
    <html>
        <head>
            <title>Logon</title>
        </head>
        <body>
            <p>Enter your login details below:</p>
        <form action="logon_input.php" method="post">
            <div><?php echo($formMessage?></div>
            <div>
            <p><?php echo(@$username['msg']) ?></p>
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" 
                    value="<?php echo(@$username['value']) ?>" />
            </div>
            <div>
            <p><?php echo(@$password['msg']) ?></p>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" />
            </div>
            <div><input type="submit" /></div>
            </form>
        </body>
    </html>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Guys what about this http://www.vbforums.com/showpost.php...65&postcount=5



    I'm trying to put it into my page here as Hobo suggested:

    Code:
    <?php
    if( user_is_logged_in ) <<<<< this bit here
    {
        require( "admin_menu.html" );  
    }
    else
    {
        require( "menu_start.html" );  
    }
    ?>

    So any advice on how to modify it...?

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Starting off with login/registration

    Depends on how you are authenticating users? Are you using sessions to store an active, logged in session? If so, then you should probably check to see if the session exists, and also verify that the session data is accurate.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    I'm not using sessions I'm using cookies - post #5 shows the sample code... What I want to do is put that code into something like logincheck.php so I can call that and find out if logged in or not.... Is that possible/good idea?

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Starting off with login/registration

    I would write a function (or method of an object) to validate the login:

    PHP Code:
    function ValidateLogin()
    {
       
    // code to verify cookie and return boolean true or false
    }

    ...

    if( 
    ValidateLogin() )
    {
        require( 
    "admin_menu.html" ); 
    }
    else
    {
        require( 
    "menu_start.html" );

    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Hmmm this is what I got in the end (for anyone looking for the same thing)

    Code:
    function cookieCheck()
    {
            if(!isset($_COOKIE['my_hash']))
            {
                    return false;
            }
            $c_user = $_COOKIE['my_username'];
            $c_hash = $_COOKIE['my_hash'];
            
            $check = mysql_query("SELECT * FROM `Users` WHERE `Username` = '$c_user' && `Userhash` = '$c_hash'") or die(mysql_error());
            if(!$check || mysql_num_rows($check) < 1)
            {
                    return false;
            }
            return true;
    }

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Starting off with login/registration

    Nice, that's probably exactly how I'd do it...although I'm an OO guy, so I'd have a class for it, but that's good.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Ohno!!! I've ruined everything it!!! Damit!

    I getting erratic results with my login pages and as I haven't really added other security measures (yet) it's hard to confirm if I'm logged in or not, especially since I'm having to refresh every page to get it to show properly....!

    Can someone please advise on a flowchart or something for doing a login check. The post above (#12) is used to check if a cookie exists but for some reason this doesn't work:

    Code:
    if(cookieCheck) { // logged in
    header("Location: index2.php");
    //print_r(cookieCheck);
    } else { // not logged in
    $past = time() - 100; 
    //this makes the time in the past to destroy the cookie 
    setcookie('my_username', '', $past); 
    setcookie('my_hash', '', $past); 
    header("Location: index.php");
    }
    What I'm doing [or trying to do] with the above code is check if the login cookies exist and if they do check their details to confirm the user is logged on ok. IF they do not exist or they do not match with whats in the db that means the user is not logged in.

    What I'm getting is, even if there is nothing in the username/password form it jumps to index2.php, in other words the line

    Code:
    if(cookieCheck) { // logged in
    header("Location: index2.php");
    seems to always run!!! That must mean cookieCheck isn't working right, so what can I change?????

    All I need is to put something in cookieCheck which will let me check if a user is logged in or not and then present TRUE or FALSE.... plz help!

  15. #15
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Starting off with login/registration

    this might help you:
    PHP Code:
    <?php
      $loggedin 
    false;
      if(isset(
    $_COOKE['my_username'], $_COOKIE['my_hash'])){
        
    //check the values against the database
        
    $user mysql_real_escape_string($_COOKIE['my_username']);
        
    $hash mysql_real_escape_string($_COOKIE['my_hash']);
        @
    extract(mysql_fetch_assoc(mysql_query("SELECT hash as authenticate FROM tablename WHERE username='$user' AND hash='$hash'")));
        if(isset(
    $authenticate)){
          
    $loggedin true;
          
    //REFRESH the cookies
          
    $renew time() + (3600 24 3); +3 days
          setcookie
    ('my_username'$_COOKIE['my_username'], $renew);
          
    setcookie('my_hash'$_COOKIE['my_hash'], $renew);
        }else{
          
    //invalid login info, set their cookies to expired
          
    $expire time() - 3600//-60 minutes
          
    setcookie('my_username''--'$expire);
          
    setcookie('my_hash''--'$expire);
        }
      }
      if(
    $loggedin)
        
    header("Location: ./index2.php");
      else
        
    header("Location: ./index.php");
      
    //you don't need to set the cookies to not exist if they aren't logged in. this will somewhat save on the amount of queries sent to the database
    just a side note: you should check if the user is logged on with an include on every page (or at least, that's what I usually do). This way, someone couldn't just type index2.php in their browser's address bar and be "logged in."

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Wow excellent! Well actually this is what I was doing, can you let me know how the above would fit in with my psuedocode:

    Code:
    1. Get hash info;
    
    2. If (COOKIEHASH <> DBHASH) // not logged in
    { *make session var with page url
    *go to login page (which will check if session var exists, if it does it will check login and then go to page url in session var)
    }
    
    3. Get Levelid // each user has a levelid
    if (Levelid <> Page levelid) // user does not have access to this page
    { die ("You do not have access to this page");
    So that's what I was thinking, I duno if it is the best way but it seems right for me at this stage...

    I would run this on every page which needs the user to be logged in. However I understand that if I have it on every page I might as well put it into a function! How can I combine it with what you have above to do just that as that will save me loads of time and space

  17. #17
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Starting off with login/registration

    well, you don't have to make a function.. you can just have an include that does it. it's a little pointless, to me, to make it a function, seeing as how you will have to include the "function" script into every page anyway; and then you'd also need to call the funciton as well. it's counter-intuitive.

    for your include, though, you could basically do this:
    authenticate.inc.php:
    PHP Code:
    <?php
      $loggedin 
    false;
      if(isset(
    $_COOKIE['my_username'], $_COOKIE['my_hash'])){
        
    $user mysql_real_escape_string($_COOKIE['my_username']);
        
    $hash mysql_real_escape_string($_COOKIE['my_hash']);
        @
    extract(mysql_fetch_assoc(mysql_query("SELECT level as authenticate FROM table_users WHERE hash='$hash' AND username='$user' LIMIT 1")));
        if(isset(
    $authenticate)){
          
    //global usage variables
          
    $loggedin true;
          
    $login = array();
          
    $login['user'] = $_COOKIE['my_username'];
          
    $login['level'] = $authenticate//this sets their level
          //refresh their cookies
          
    $renew time() + (3600 24 3); //+3 days
          
    setcookie('my_username'$_COOKIE['my_username'], $renew);
          
    setcookie('my_hash'$_COOKIE['my_hash'], $renew);
        }else{
          
    //bad login, expire their cookies
          
    $expire time() - 3600//-60 minutes
          
    setcookie('my_username''--'$expire);
          
    setcookie('my_hash''--'$expire);
        }
      }

      
    //if they aren't logged in, let's redirect them
      
    if(!$loggedin)
        
    header("Location: ./login.php");

      
    //if they ARE logged in, everything else included AFTER this page will show up fine
    ?>
    sample use:
    index.php:
    PHP Code:
    <?php
      
    require_once("mysql.inc.php"); //mysql database information/login
      
    require_once("authenticate.inc.php"); //authentication script above
      //set this page's values (can easily set all of this stuff with a database query, too)
      
    $this['level'] = 5;
      if(
    $login['level'] < $this['level'])
        die(
    "You aren't authorized to view this page!");
    ?>
    <html>
      <head>
        <title>Super Secret Location</title>
      </head>
      <body>
        <h1>hello, <?php echo $login['user']; ?>!</h1>
      </body>
    </html>

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Lol youre right, I didn't mean a function - I meant an include (similar to a subroutine where you can just write it once but call it many times)!!

    Thx for the code I will test it in my pages and see what I come up with

  19. #19
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    Re: Starting off with login/registration

    Well, a while back I wrote a very secure login script that allows basically everything you need. I outsourced it to many people on my web host's forums and they used it quite often and gave it some great reviews.

    Take a look at it, it is completely open-source, I do not require credit as long as you are not taking credit for it.

    The source is EXTREMELY simple and includes some pretty nice functions for redirecting without headers. I would check it out, and tell me if you like it or not. I have included it as an attachment. But the code is also below:

    Index.php
    PHP Code:
    <?
    session_cache_expire(30);
    session_start();
    //error_reporting(2047);
    ?>
    <style type="text/css">
    <!--
    .style1 {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 9px;
    }
    -->
    </style>


    <form id="form1" name="form1" method="post" action="index.php?action=login">
      <table width="234" border="0" align="center">
        <tr>
          <td width="209" bgcolor="#FFFFFF"><div align="center"><img src="images/Banner.png" width="468" height="60" border="2" /></div></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><table width="150" border="0" align="center">
            <tr>
              <td>Username</td>
              <td><input type="text" name="username" /></td>
            </tr>
            <tr>
              <td>Password </td>
              <td><input type="password" name="password" /></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><div align="center">
            <input type="submit" name="Submit" value="Submit" />
            <label>
            <input type="reset" name="Submit2" value="Reset Form" />
            </label>
          </div></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><div align="center" class="style1">&copy;2006 by Seraphino. All Rights Reserved. Outsourced For Individuals. Version 1.1. </div></td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF">&nbsp;</td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><div align="center"><span class="style1">Register Today!  </span></div></td>
        </tr>
      </table>
      <label></label>
      <p>
        <label></label>
      </p>
      <hr />
      <?php
    //Main Variables Declarations
    $action $_GET['action'];
    $doing $_GET['doing'];
    $username1 $_POST['username'];
    $password1 $_POST['password'];

    //All The Doings of A Login Script

    if($action == "login"
    {
    //The MySQL Variables
    $server "yourmysqlserver";
    $username "db_username";
    $password "db_password";
    $database "db_name";
    echo 
    "Currently Logging You In...";

    mysql_connect($server,$username,$password);
    @
    mysql_select_db($database) or die("Unable to select database");

    $query="SELECT * FROM users";
    $result=mysql_query($query);
    $num=mysql_numrows($result);

    //Checks If The Username and Password Exists
    //Declaring Variables
    $usernameisvalid=0;
    $passwordisvalid=0;
    $passwordexists=0;
    $usernameexists=0;
    //loop through all the entrys in the database to see if any match...
        
    while ($usernameisvalid $num
        {
            
    //set the username they want to a variable
            
    $checkuser=$username1;
            
    //Set the database user name into a variable

            
    $existing=mysql_result($result,$usernameisvalid,"Username");


            
    //Check whether or not they are the same

                
    if  ($checkuser == $existing)
                {
                    
    $usernameexists=1;
                    
    $DBspotnew=$usernameisvalid;
                    
    $usernameisvalid=$num;            
                } 
                    
                    
    $usernameisvalid++;
                
        }
        while (
    $passwordisvalid $num
        {
            
    //Set Password They Want Into A Variable
            
    $checkpassword=$password1;

            
    //Set the database password into a variable
            
    $existingpass=mysql_result($result,$passwordisvalid,"Password");

            
    //Check whether or not they are the same
            
                
    if ($checkpassword == $existingpass)
                {
                    
    $passwordexists=1;
                    
    $DBspot $passwordisvalid;
                    
    $passwordisvalid=$num;
                } 
                    
                    
    $passwordisvalid++;
                
        }
        
        if (
    $passwordexists == or $usernameexists == 0)
        {
            echo 
    "This Username or Password is Invalid!";
        }
        if (
    $usernameexists == and $passwordexists == 1)
        {
            echo 
    "Loading Members Page...";
            
    $_SESSION['LoggedIn'] = True;
            
    $_SESSION['UserName']= mysql_result($result,$DBspotnew,"Username");
            
    $_SESSION['PassWord']= mysql_result($result,$DBspotnew,"Password");
            
    mysql_close();
            
    js_redirect_loggedin();
        }
    }

    if(
    $action == "loggedin"
    {
        if(isset(
    $_SESSION['LoggedIn'])) 
        { 
    //Checks if the session named "Logged In" is set to 1
            
    include('memberspage.php');
        }
        else 
        {
            echo 
    "You are not logged in!";
        }
    }

    if(
    $action == "loggedout")
    {
        echo 
    "Logging Out...";
        
    session_destroy();
        
    js_redirect_loggedout();
    }

    //Redirect Function

    function js_redirect_loggedin($url="YourURL"$seconds=3) { 
        echo 
    "<script language=\"JavaScript\">\n"
        echo 
    "<!-- hide from old browser\n\n"
         
        echo 
    "function redirect() {\n"
        echo 
    "window.location = \"" $url "\";\n"
        echo 
    "}\n\n"

        echo 
    "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n"

        echo 
    "-->\n"
        echo 
    "</script>\n";

        return 
    true;
    }

    function 
    js_redirect_loggedout($url="YourURL"$seconds=3) { 
        echo 
    "<script language=\"JavaScript\">\n"
        echo 
    "<!-- hide from old browser\n\n"
         
        echo 
    "function redirect() {\n"
        echo 
    "window.location = \"" $url "\";\n"
        echo 
    "}\n\n"

        echo 
    "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n"

        echo 
    "-->\n"
        echo 
    "</script>\n";

        return 
    true;
    }
    ?>
    </form>

    <p>&nbsp; </p>
    memberspage.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <
    title>Untitled Document</title>
    <
    style type="text/css">
    <!--
    #Layer1 {
        
    position:absolute;
        
    width:765px;
        
    height:369px;
        
    z-index:1;
        
    left208px;
        
    top16px;
    }
    -->
    </
    style>
    </
    head>

    <
    body>
    <
    table width="185" border="0">
      <
    tr>
        <
    td width="179" bgcolor="#00FFFF"><strong>Members Nav Menu</strong></td>
      </
    tr>
      <
    tr>
        <
    td bgcolor="#00FFFF"><ul>
          <
    li><a href=#>Homepage</a></li>
          
    <li><a href=#>View Account Details</a></li>
          
    <li><a href="LogoutLocation(typically ?action=logout)">Log Out</a></li>
        </
    ul></td>
      </
    tr>
    </
    table>
    </
    body>
    </
    html
    viewacctinfo.php
    PHP Code:
    <?php
    echo "Your username is:" $_SESSION['UserName'] . "";
    echo 
    "Your password is:" $_SESSION['PassWord'] . "";
    ?>
    If you did not notice, you have to change some things, take a look through the code and do not use it directly or it will not work, you have to suit it to your likings.
    Attached Files Attached Files



  20. #20
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Starting off with login/registration

    I hope the following doesn't come off to you as an attack, Seraphino; I am just curious and am very confused as to why you did your login script the way you did.

    first of all: it is VERY, VERY, VERY inefficient for you to loop through EVERY record in a table just to check if some user exists. the overhead from PHP alone will add tons of executing time to your script depending on how many users you have; and what if you had a million? you should be using your WHERE clause to select only the record you want (the record that has a username equal to $_POST['username'] and also has a password equal to $_POST['password']), and let MySQL do ALL of the work for you. doing it in PHP is not more secure, or not more anything. It's more work that you don't need to do. Make sure you validate the user input, too (eg: functions like mysql_real_escape_string()), so that you can protect yourself from SQL injection.

    how is what you posted "very secure" if memberspage.php doesn't even check for sessions or anything? someone could just type that address into their browser and view it.

    also, using PHP's headers is a much better idea if you're doing it before any output has been given to the browser; not all users have javascript enabled. some old browsers don't even support the javascript redirect you used. it's much, much easier to use a built-in PHP function than to make two functions that just output javascript. if you're making an administration module, you don't want anyone who shouldn't have the ability to potentially view anything, so using headers for redirection is great.

    a few more questions/comments:
    • what's the point in having your stylesheet at all in memberspage.php if you don't use the #layout entity?
    • you shouldn't be including a page that has all the making of an HTML page into a page that already has its own DOM (eg: your index.php always outputs <html> and <body> tags, and it's incorrect for you to be including a new file with its own <html> and <body> tags (memberspage.php))
    • did you not post the full source of these pages..? or am I missing something?

  21. #21
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    Re: Starting off with login/registration

    I don't consider it an attack. What I was mainly talking about with security is the login itself. The members page was barely worked on by myself and I count on many people to replace it with what they want because it is really nothing.

    Also, looping through the database takes under 2 seconds even with over 500 users in the database, so it is not like it is going to take an hour to do something like that.

    Of course, I always planned on redoing the script anyway because there are many things I would want to upgrade on the script.



  22. #22
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Starting off with login/registration

    Quote Originally Posted by Seraphino
    Well, a while back I wrote a very secure login script that allows basically everything you need. I outsourced it to many people on my web host's forums and they used it quite often and gave it some great reviews.
    As Mr T would say: "I pity the fools".


    Quote Originally Posted by Seraphino
    Also, looping through the database takes under 2 seconds even with over 500 users in the database
    Try with 50,000+ users. Or a slow connection to the database. Returning the resultset alone will kill it.

    Also, doing anything that can be done using headers, by other means, is bad.


    Quote Originally Posted by Seraphino
    Of course, I always planned on redoing the script anyway because there are many things I would want to upgrade on the script.
    Why post it then?


    Quote Originally Posted by kows
    Make sure you validate the user input, too (eg: functions like mysql_real_escape_string()), so that you can protect yourself from SQL injection.
    Use a proper data access library that supports parameterised queries, then you never have to mess around escaping anything.

    Also, with regards to the code kows posted, remember in a production environment to always validate the results of functions such as mysql_query and mysql_fetch_assoc, and not to use die() error messages.

  23. #23
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Starting off with login/registration

    Quote Originally Posted by Seraphino
    Well, a while back I wrote a very secure login
    I love how you don't even have to login to access the members page


    Quote Originally Posted by Seraphino
    I outsourced it to many people on my web host's forums and they used it quite often and gave it some great reviews.
    Maybe they should learn PHP.

    Quote Originally Posted by Seraphino
    The source is EXTREMELY simple
    That has got to be the most complex login script I have EVER seen.

    Quote Originally Posted by Seraphino
    and includes some pretty nice functions for redirecting without headers.
    Redirecting using Javascript is in no way favourable over using the builtin headers provided by HTTP and browsers with Javascript disabled will not work at all.

    Quote Originally Posted by Seraphino
    If you did not notice, you have to change some things.
    Personally, I would rewrite the entire script.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Starting off with login/registration

    Thanks loads guys!!! In the end this is what I've gone with, I'm sure its not the most advanced solution but I guess it does the job:

    On each page which needs authentication I've added the following lines:

    Code:
    ob_start();
    at the top

    Code:
    require_once("auth.php");
    
    // check levelid is ok for this page
    if ($db_level<>1) // user doesnt have access
    {
    die("You don't have access to this page");
    }
    in the middle, the check level is different for different pages of course

    and

    Code:
    ob_end_flush();
    at the bottom



    I've been testing this and it seems ok... so cool

  25. #25
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Starting off with login/registration

    Quote Originally Posted by Seraphino
    Also, looping through the database takes under 2 seconds even with over 500 users in the database, so it is not like it is going to take an hour to do something like that.
    You need more than one second to find one particular record among 500? That is, of course, if only one user is accessing the database at any time.

    And no alarm bells are ringing? Hell, they should be falling off the bell tower!

    For reference, doing this the proper way typically takes a database server around a millisecond on your typical "I converted an old computer" development server. Your script is three orders of magnitude slower.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  26. #26
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    Re: Starting off with login/registration

    Yeah, I know this is a flame war against me, but if you didn't notice, this was one of the first scripts that I ever wrote from memory of PHP (no book or anything).

    Plus, I believe I mentioned that I planned on re-writing it someone to take into account all the skills I have acquired since I first wrote that script.



  27. #27
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Starting off with login/registration

    but if you're still handing out that script and trying to defend it, you can't be trying to say you've acquired a lot of skills since you first wrote that script.. if you did, you would know how wrong it was.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width