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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.