Jump to content

Using a logout script (user has to click the logout link twice)


j.smith1981

Recommended Posts

I am having problems understanding the reason for why the user has to click logout twice, here's the bulk of the code:

 

<?php
ini_set('display_errors',0);

require_once 'header.html';

require_once 'db.functions.php';

require_once 'config.php';

$database = dbConnect($host, $username, $password, $database); // should output 1 or nothing at all!

if($database == true)
{
// now connected?
// carry on with logic of outputting the blog contents:
$result = entries("SELECT * FROM entries");

printf("<table>");

while($row = mysql_fetch_array($result))
{
	printf("
	<tr>
	<td>%s</td>	<td>%s</td>
	</tr>

	<tr>
	<td colspan=\"2\">%s</td>
	</tr>
	", $row[2], $row[4], $row[3]);
}

printf("</table>");

printf("\n\n");

session_name("jeremysmith_blog");
session_start();

if(array_key_exists('login',$_SESSION))
{
	if($_SESSION['login'] == 1) // change this to correspond with session on the login.php script
	{

		printf("<p>Welcome %s</p>

		<p>To logout, click <a href=\"index.php?action=logout\">here</a></p>

		",$_SESSION['username']);

	}

} else {
	printf("<p>You are not logged in, please click <a href=\"login.php\">here</a> to login.</p>");
}


} else {

printf("\n<p id=\"error\">Could not connect to database, please try again later.</p>");

}

// init the logout script?
if(array_key_exists('action',$_GET))
{
if($_GET['action'] == 'logout')
{
	// log user out of the system:
	unset($_SESSION['login']);
	unset($_SESSION['username']);
	session_destroy();
}
}


printf("\n"); // just for output format!
require_once 'footer.html';

 

Why does the user have to click logout twice, have I missed anything?

 

Any helps appreciated thanks.

Move these lines

<?php
session_name("jeremysmith_blog");
session_start();
?>

to right after the opening "<?php". Since you currently have them after you send output to the browser, the session isn't being started correctly.

 

Ken

Of course!

 

Sorry heads not in gear, sorting out my login security, getting some much needed advice on this and I forgot that part.

 

Nice one, thank you ever so much!

 

Jez.

No its still doing that, where the user has to click twice, I now have this:

 

<?php
ini_set('display_errors',0);

session_name("jeremysmith_blog");
session_start();

require_once 'header.html';

require_once 'db.functions.php';

require_once 'config.php';

$database = dbConnect($host, $username, $password, $database); // should output 1 or nothing at all!

if($database == true)
{
// now connected?
// carry on with logic of outputting the blog contents:
$result = entries("SELECT * FROM entries");

printf("<table>");

while($row = mysql_fetch_array($result))
{
	printf("
	<tr>
	<td>%s</td>	<td>%s</td>
	</tr>

	<tr>
	<td colspan=\"2\">%s</td>
	</tr>
	", $row[2], $row[4], $row[3]);
}

printf("</table>");

printf("\n\n");

if(array_key_exists('login',$_SESSION))
{
	if($_SESSION['login'] == 1) // change this to correspond with session on the login.php script
	{

		printf("<p>Welcome %s</p>

		<p>To logout, click <a href=\"index.php?action=logout\">here</a></p>

		",$_SESSION['username']);

		print_r($_SESSION);

	}

} else {
	printf("<p>You are not logged in, please click <a href=\"login.php\">here</a> to login.</p>");
}


} else {

printf("\n<p id=\"error\">Could not connect to database, please try again later.</p>");

}

// init the logout script?
if(array_key_exists('action',$_GET))
{
if($_GET['action'] == 'logout')
{
	// log user out of the system:
	session_unset($_SESSION['login']);
	session_unset($_SESSION['username']);
	session_unset($_SESSION['type']);
	session_destroy();
}
}


printf("\n"); // just for output format!
require_once 'footer.html';

 

Have I obviously missed something sorry?

 

Jez.

You're doing the login check and outputting the logout link before you're logging the user out, so they are being logged out on the first click; the display just isn't reflecting that until the reload..  Move the logout piece up to the top right after the session_start() cal.l

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.