Login and Logout using Sessions and Cookies

Discussion in 'PHP' started by shabbir, Jun 15, 2005.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Any encryption is fine till you test both using the same encryption
     
  2. caca

    caca New Member

    Joined:
    Oct 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    im having problems... i tried it using crypt

    functions.php
    PHP:
    <?php
    include("connection.php"); //it has the database info
    function createsessions($username,$password)
    {
        
    //Add additional member to Session array as per requirement
        
    session_register();

        
    $_SESSION["gdusername"] = $username;
        
    $_SESSION["gdpassword"] = crypt($password);
        
        if(isset(
    $_POST['remme']))
        {
            
    //Add additional member to cookie array as per requirement
            
    setcookie("gdusername"$_SESSION['gdusername'], time()+60*60*24*100"/");
            
    setcookie("gdpassword"$_SESSION['gdpassword'], time()+60*60*24*100"/");
            return;
        }
    }

    function 
    clearsessionscookies()
    {
        unset(
    $_SESSION['gdusername']);
        unset(
    $_SESSION['gdpassword']);
        
        
    session_unset();    
        
    session_destroy(); 

        
    setcookie ("gdusername""",time()-60*60*24*100"/");
        
    setcookie ("gdpassword""",time()-60*60*24*100"/");
    }

    function 
    confirmUser($username,$password)
    {
        
    //$md5pass = md5($password);

        
    $query 'SELECT username,pass FROM accounts WHERE username='.$username;
        
    $result mysql_query($query);
        
    $data mysql_fetch_assoc($result);
        
    $md5pass crypt($password);


        
    //if($username == "demo" && $password = "demo")
        
    if($username == $data[username] && $password $data[pass]) 
            return 
    true;
        else 
            return 
    false;
    }

    function 
    checkLoggedin()
    {
        if(isset(
    $_SESSION['gdusername']) AND isset($_SESSION['gdpassword']))
            return 
    true;
        elseif(isset(
    $_COOKIE['gdusername']) && isset($_COOKIE['gdpassword']))
        {
            if(
    confirmUser($_COOKIE['gdusername'],$_COOKIE['gdpassword']))
            {
                
    createsessions($_COOKIE['gdusername'],$_COOKIE['gdpassword']);
                return 
    true;
            }
            else
            {
                
    clearsessionscookies();
                return 
    false;
            }
        }
        else
            return 
    false;
    }
    ?>
    login.php
    PHP:
    <?php

    ob_start
    ();
    session_start();

    include (
    "functions.php");

    $returnurl urlencode(isset($_GET["returnurl"])?$_GET["returnurl"]:"");
    if(
    $returnurl == "")
        
    $returnurl urlencode(isset($_POST["returnurl"])?$_POST["returnurl"]:"");

    $do = isset($_GET["do"])?$_GET["do"]:"";

    $do strtolower($do);

    switch(
    $do)
    {
    case 
    "":
        if (
    checkLoggedin())
        {
            echo 
    "<H1>You are already logged in - <A href = \"login.php?do=logout\">logout</A></h1>";
        }
        else
        {
            
    ?>
            <form NAME="login1" ACTION="login.php?do=login" METHOD="POST" ONSUBMIT="return aValidator();">
            <input TYPE="hidden" name="returnurl" value="<?$returnurl?>">
            <TABLE cellspacing="3">
            <TR>
                <TD>Username:</TD>
                <TD><input TYPE="TEXT" NAME="username"></TD>
                <TD>Password:</TD>
                <TD><input TYPE="PASSWORD" NAME="password"></TD>
            </TR>
            <TR>
                <TD colspan="4" ALIGN="center"><input TYPE="CHECKBOX" NAME="remme">&nbsp;Remember me for the next time I visit</TD>
            </TR>
            <TR>
                <TD ALIGN="CENTER" COLSPAN="4"><input TYPE="SUBMIT" name="submit" value="Login"></TD>
            </TR>
            </form>
            </TABLE>
        <?
        }
        break;
    case "login":
        $username = isset($_POST["username"])?$_POST["username"]:"";
        $password = isset($_POST["password"])?$_POST["password"]:"";

        if ($username=="" or $password=="" )
        {
            echo "<h1>Username or password is blank</h1>";
            clearsessionscookies();
            header("location: login.php?returnurl=$returnurl");
        }
        else
        {
            if(confirmuser($username,crypt($password))) 
            {
                createsessions($username,$password);
                if ($returnurl<>"")
                    header("location: $returnurl");
                else
                {
                    header("Location: menu.php");
                }
            }
            else
            {
                echo "<h1>Invalid Username and/Or password</h1>";
                clearsessionscookies();
                header("location: login.php?returnurl=$returnurl");
            }
        }
        break;
    case "logout":
        clearsessionscookies();
        header("location: menu.php");
        break;
    }
    ?>
    menu.php - i just renamed it
    PHP:
    <?php
    ob_start
    ();
    session_start();

    require_once (
    "functions.php");

    if (
    checkLoggedin())
        echo 
    "<H1>You are already logged in as $_SESSION[gdusername] - <A href = \"login.php?do=logout\">logout</A></h1>";
    else
        echo 
    "<H1>You are not logged in - <A href = \"login.php\">login</A></h1></h1>";
    ?>
    connection.php
    PHP:
    <?
    $user="root";
    $pw="";
    $db="isee12do_TwelveDots";

    mysql_connect("localhost",$user$pw);
    mysql_select_db($db) or die("Cannot connect with database".mysql_error());
            
    ?>
     
  3. bryansmith

    bryansmith New Member

    Joined:
    Sep 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.indiansoftwareoutsourcing.com/
    Thanks for sharing. please help me to know more about php. Am new to this forum and interested to learn php.
     
  4. davidolivamailx

    davidolivamailx New Member

    Joined:
    Nov 10, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi shabbir my name is sumon and i m from bangladesh ..i m new php programmer..i used loging and logout code without clearing cookie and session..your post is so helpful for me..thanks a lot for this nice code..i m now wanted to use this to my web site ..one last thing.can u help me to using multiple session??

    thanks a lot
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What do you mean by multiple session
     
  6. ea2010

    ea2010 New Member

    Joined:
    Nov 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi I really cant get this working ! When you go to the index page it says wether you are logged in our not but you can still see the whole page wether you are logeed in or not how would i change this, also when I selct the databse etc using both variations of the code posted earlier in the topic it just keeps returning to the login with no error messages please could someone help would really appreciate it :) thanks in advance hope I can get this working :)
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have to start the session on pages where you want the status.
     
  8. JV2010

    JV2010 New Member

    Joined:
    Nov 12, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  9. rgbdevel

    rgbdevel New Member

    Joined:
    Nov 30, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi.
    Im having trouble with killing the session.
    If i try and login and DON'T check the checkbox for remeber me next time,
    it still remembers me as loggedin ?
    Any one know why? I use firefox 3.5.5, PHP Version 5.2.6-1+lenny3, Apache 2.0
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Do you mean when user clicks logout.

    If you want that user logs out after some time you need to have the time limit after which session expires.
     
  11. loyo

    loyo New Member

    Joined:
    Feb 12, 2010
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://bbs.prog365.com
    Oh, this topic is so hot.
    come on
     
  12. lmdsb

    lmdsb New Member

    Joined:
    Apr 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I've been trying to find out how to start a new thred but I couldn't fund anything so I'm posting my question here...

    Hi, I'm trying to build an e-commerce website with the following features:
    Login functionality
    Shopping cart functionality
    Product search feature
    Recently Viewed Items
    Rate products
    Add comments
    View transaction history
    Have a community forum
    Email alerts

    I plan to build this website using Dreamweaver. Can you please tell me:
    1. Is it possible to build the above site using Dreamweaver? If so how?
    2. How do I get about it?
    3. Are there any step by step tutorials to help me?
    4. Preferably are there any sites that give free and speedy assistance?
    Thanks
     
  13. ssk1712

    ssk1712 New Member

    Joined:
    Jun 17, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    The code which you provided has some error like this:::

    Parse error: syntax error, unexpected $end in C:\xampp\htdocs\createsession\login.php on line 81

    this is the error which I'm getting.Can you please help me?or post the correct code.
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    81 is the last line of the code and it only contains ?>
     
  15. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    http://www.entheosweb.com/dreamweaver/default.asp
    http://www.thesitewizard.com/gettingstarted/dreamweaver1.shtml




     
  16. ssk1712

    ssk1712 New Member

    Joined:
    Jun 17, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    yes,after searching on net I found there is a bracket missing.But I'am not able to solve the issue.can you please help him to solve.or can you provide me a similar code.I need a code in which there should be session expired(ie: time out you have to login again some what code).
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What do you mean by missing bracket?

    Also the code uses the session as well as cookie and session expiring depends on your web server configuration.
     
  18. ssk1712

    ssk1712 New Member

    Joined:
    Jun 17, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Yes iknow but whenever I search the error I get missing bracket.please can you look into the code and let me know.im waiting for the code to get corrected but Im always getting the same error.
     
  19. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    It works on my end and so I am not sure if it needs any correction.

    If it says a missing braces add one and see if it works but I have checked it yet again to see no errors.
     
  20. ssk1712

    ssk1712 New Member

    Joined:
    Jun 17, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    yes I tried putting a bracket into the code but still the same thing.I dont think its going to work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice