[SOLVED] Help with login/logout script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Help with login/logout script

Post by Getran »

Hey, i have my login and signup pages finished and everything. So i made a logout link too, when they go on it, i get the error: Underfined variable: username. How can i make it remember the $username var from login.php ? i tried just doing global $username; but i think that only global's it for that page ?

It shows this error, but does actually log them out too, can someone tell me how to sort this out plz ? :)

// Getran
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Use sessions. on pages after the login how do you validate the user?
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

i don't quite understand what u mean, but this is to check whether they're logged in: (btw i'm using cookies not sessions)

Code: Select all

<?
 $checkcookie = $_COOKIE["terrastormlogin"];
    if (isset($checkcookie))
    {
 		$loggedin = "1";
		global $loggedin;
		global $username;
		$maincontent = "";
		$maincontent .= "Cookie is set";
    }
    else if (!isset($checkcookie))
    {
    	$loggedin = "0";
        global $loggedin;
        $maincontent = "";
        $maincontent .= "Cookie Not Set";
    }
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

When they log-out. change the expire date of your cookie:

Code: Select all

<?php
	//KILL COOKIE
	$tm =time()-60*60*24*30;
	setcookie("my_cookie_name", $some_variable,$tm,'/');
?>

Edit: Take a look at the following tuturials for sessions:

http://www.phpexamples.net/tutorial-8-1.html
http://www.digiways.com/articles/php/sessauth/
http://www.phptutorial.info/learn/session.html
Last edited by hawleyjr on Thu Aug 12, 2004 7:16 am, edited 1 time in total.
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

it aint that, that's the problem, it logs out find as i have it. but i get undefined var error... the logout page is at its simplest atm..:

<?
setcookie("terrastormlogin", "$username", -3600);
echo "You have been logged out!";
?>

and even the $username var is undefined, it still logs them out...

*EDIT* - Maybe i should just use sessions...lol, might be a bit easier :/
Last edited by Getran on Thu Aug 12, 2004 7:19 am, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

What is the specific error you are getting?
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

Notice: Undefined variable: username in *hidden*\logout.php on line 2
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Please show more of your code. Have you tried setting username to some bogous name?

Code: Select all

<?php
$username = 'Yo Mamma';
setcookie("terrastormlogin", "$username", -3600); 
echo "You have been logged out!"; 
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Feyd, please mark as solved. Solved through PM.
Post Reply