Help with login script (session/cookie)...

This is a discussion on "Help with login script (session/cookie)..." within the PHP Forum section. This forum, and the thread "Help with login script (session/cookie)... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 15th, 2008, 22:30
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Help with login script (session/cookie)...

Hi, I was wondering if somebody could help me with my LOGIN script!
This is mostly from a tutorial, but I canīt seem to make it do what I want.

I would like for the user so be able to sign in and stay signed in for 6 hours. Right now, when user logs-in he STAYS logged in as long as he doesn't leaver the root page or sub-pages. If he visits Google in the same browser window/tab and goes back the user isn't logged in anymore.

Hoe can I fix these issues with the code below?
ISSUE 1 - User stays logged in when leaving site / closing browser.
ISSUE 2 - User signs out when session is over. When on or off site.

Hereīs my LOGIN page code:
PHP: Select all

<?php
require_once('Scripts/dbconnect.php');

$dbops = New MyOps;
$link $dbops->dbconnect();

session_start();            // Shows we are using sessions

$username $_POST['username'];            // Gets the inputted username from the form
$password $_POST['password'];            // Gets the inputted password from the form
$time time();                            // Gets the current server time

$query "SELECT user, pass FROM login WHERE user = '$username' AND pass = '$password'";
$result mysql_query($query$link);

if(
mysql_num_rows($result))
{
    
// If the username and password are correct do the following;
    
$_SESSION['loggedin'] = 1;        // Sets the session 'loggedin' to 1
    
$cookie 1;
    
    
$access "Y";
    
    if(
$cookie == 1)
    {
        
// Check to see if the 'setcookie' box was ticked to remember the user
        
setcookie("Throunarsaga[username]"$username$time 21600);        // Sets the cookie username
        
setcookie("Throunarsaga[password]"$password$time 21600);        // Sets the cookie password
        
    
}
   
}
else        
// If login is unsuccessful forwards the user back to the index page with an error
{
    
$access "N";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>

<?php
    
    
if ($access == "Y"
    {
        echo 
"You are logged in!";
        
    }
        else
        {
        
        
$reload $_SERVER['PHP_SELF']; 
        echo 
"<form action=\"$reload\" method=\"post\">
                <table border=\"0\" align=\"center\" bgcolor=\"#000000\">
                <tr>
                  <td colspan=5>&nbsp;</td>
                </tr>
                <tr>
                  <td width=\"137\">&nbsp;</td>
                  <td width=\"57\">User:</td>
                  <td colspan=\"2\"><input type=\"text\" name=\"username\" size=\"20\" maxlength=\"40\" /></td>
                  <td width=\"46\">&nbsp;</td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>Pass:</td>
                  <td colspan=\"2\"><input type=\"password\" name=\"password\" size=\"20\" maxlength=\"50\" /></td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td colspan=\"2\">&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td width=\"22\">&nbsp;</td>
                  <td width=\"166\"><input type=\"submit\" name=\"Login\" value=\"Login\" /></td>
                  <td>&nbsp;</td>
                  </tr>
                <tr><td colspan=\"5\" align=\"right\">&nbsp;</td></tr>
                </table>
                </form>"
;
        }
?>
Reply With Quote

  #2 (permalink)  
Old Feb 15th, 2008, 22:43
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Help with login script (session/cookie)...

You can log the user out with session_destroy(); But you cannot choose how long the cookie remains on their computer really.
The client's browser has control over the cookie's expiration.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #3 (permalink)  
Old Feb 15th, 2008, 23:05
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with login script (session/cookie)...

Quote:
Originally Posted by alexgeek View Post
You can log the user out with session_destroy(); But you cannot choose how long the cookie remains on their computer really.
The client's browser has control over the cookie's expiration.
I have a logout button (with session_destroy()).
But what about pages like this one, that keeps me logged in if I check the Remember me button?

Based on this tutorial, this should be possible.
Quote:
What is a cookie anyways? It is a temporary file that is stored on the user's computer on behalf of the website in order to hold information that is important to the website. How long does this temporary file last? As long as we say so. As written, the expiry time is 100 days, after which the cookie will be deleted. However, it also gets deleted when the user decides to log out, as you will soon see.
I might just try and work from this script in the tutorial from the link above, but wouldn't I just end with the same problem? As Iīm already using the 'setcookie' as in the tutorial.
Reply With Quote
  #4 (permalink)  
Old Feb 16th, 2008, 14:07
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Help with login script (session/cookie)...

The above tutorial you have linked to does work, I have used it in the past.
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
login script mbarr Databases 13 Mar 12th, 2008 03:39
login script help Aaron1988 PHP Forum 2 Oct 25th, 2006 15:03
Login Script... something wrong! snowangel PHP Forum 6 Mar 6th, 2006 15:27
PHP Login script advice chimp PHP Forum 1 Dec 21st, 2005 17:16
client login script? godonlyknows PHP Forum 6 Jul 23rd, 2005 17:47


All times are GMT. The time now is 02:02.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43