Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Auto logged in?

  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Auto logged in?

    got a problem, when i login into my site and then close the website, i view the profile and it says still logged in, but when i go back onto the site it tells me to login again, is there a way so that when i go back to the website im still logged in until i logout?

    thanks

    Kevin

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    is there a way so that when i go back to the website im still logged in until i logout?
    login is purely a conceptual thing and we can't help until you provide the details of your login-system or some relevant code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay sorry, here is my login script.

    Login :
    PHP Code:
    $db mysql_connect($dbHost,$dbUser,$dbPass); // Connection Code
    mysql_select_db($dbname,$db);                 // Connects to database

    if(isset($_POST['submit']))
    {
        
    $username $_POST['username'];
        
    $password $_POST['password'];
        
        
    $sql "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
        
    $que mysql_query($sql);
        
        if(
    mysql_num_rows($que) == 1
        {    
            
    $sql2 "SELECT * FROM `users` WHERE `id` = '$username' ";
            
    $qry mysql_query($sql2) or die(mysql_error());
            
    $post mysql_fetch_array($qry);
        
            
            
    $get_id "SELECT `id` FROM `users` WHERE `username` = '$username' ";
            
    $info mysql_query($get_id) or die(mysql_error());
            
    $row mysql_fetch_array($info);
        
            
    $sql1 "UPDATE `users` SET active='1' WHERE ID ='$row[id]' ";
            
    $ins mysql_query($sql1) or die(mysql_error());
            
            
    $_SESSION['username'] = $_POST['username'];
            
    header("Location: profile.php?id=$row[id]");
        }else
        {
            echo 
    "<font color=\"red\"> Error Wrong Login Details </font>";
            echo 
    "<a href=\"login.php\">Login Page</a>";
        }
    }
    ?> 

  • #4
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    you'll either need $_SESSION or $_COOKIE handling i would think.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Use SESSION, it is 10X better then COOKIES and have higher security.

  • #6
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    but less persistant, right?

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Use $_COOKIE if you want to be logged back in after closing your browser, if not use $_SESSION, read up on the php.net website.

  • #8
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Also, I HIGHLY recommend you add some data checking on your username and password fields. As it appears in this script (assuming no magic quote escapes) a user would be able to maliciously override the password requirement of your field and gain access to your administration.

    Try $username = addslashes( html_entities( $_POST['username'] ) ); on the username and password at the very least!
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #9
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts

  • #10
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay, thanks for the security information, so i should use cookies for auto remembers?

    Kevin

  • #11
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    you can use cookies for auto remembers, but not to store their password.

  • #12
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay, can u give me show me how please? im no good with cookies xD

    Kevin

  • #13
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    you got to read up the implementation first(the link on top)...

    With this you can auto login the user at any page, just like how you are remembered by this forum..

  • #14
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I read it, but doesnt tell me how to do cookies xD

    i have done

    setcookie("user", "username", time()+3600);

    would this work?

  • #15
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    yes that would set a cookie call user, with the value of username and expire in a hour..

    http://php.net/setcookie


    get a firefox and use this plugin:
    https://addons.mozilla.org/en-US/firefox/addon/60

    You can see your cookie with this, it is a must for web developer.

  • Users who have thanked kokjj87 for this post:

    Kev0121 (03-26-2009)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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