Help plz logout button without changing page

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
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Help plz logout button without changing page

Post by tombruton69 »

I am trying to do this by setting a variable and then using an if statment. However it just doesnt seem to run bin looking at it for hours. Sorry for being a Noob

Code: Select all

<?php


$loginface="<h4>Register<h4/>
			<form action='home.php' method='post'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
			</form>
			</div>
			<a href='index.php?$logout=true'>";

$logout;			
//$logoutface="<br/><a href='index.php?$logout=1'>Logout</a>";
$logoutface="<form method=post action='index.php?$logout=1'>
<input type='submit' name='logout>
</form>";


if ($_SESSION['username'])
		{
		print "Hello ";
		echo  $_SESSION['username'];
		print "$logoutface";
		
		
		}
	else 
	print "$loginface";		

if ($logout=='1')
	{
  session_destroy();
  $logout="0";
  }
	

	

	?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help plz logout button without changing page

Post by Jonah Bron »

I don't fully understand your code or what you're trying to do, but what you're doing there doesn't work in at least one way. You can't do this:

Code: Select all

<a href='index.php?$logout=true'>";
And then access the variable with just $logout. You have to do it like this:

Code: Select all

<a href='index.php?logout=true'>";
And access that value with $_GET['logout'].
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

Thanx for that I am nearly there. It logs out but I have to press logout twice?

Code: Select all

<?php


$loginface="<h4>Register<h4/>
			<form action='home.php' method='post'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
			</form>
			</div>
			<a href='index.php?$logout=true'>";

$logout;			
//$logoutface="<br/><a href='index.php?$logout=1'>Logout</a>";
$logoutface="<form method=post action='index.php?logout=true'>
<input type='submit' value='logout'>
</form>";


if ($_SESSION['username'])
		{
		print "Hello ";
		echo  $_SESSION['username'];
		print "$logoutface";
		
		
		}
	else 
	print "$loginface";		

if ($_GET['logout']==true)
	{
  session_destroy();
  $_GET['logout']=false;
  }
	

	

	?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help plz logout button without changing page

Post by Jonah Bron »

[text]<a href='index.php?$logout=true'>[/text]
Change it to this
[text]<a href='index.php?logout=true'>[/text]
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

It still doesnt work thanx for your help tho

Code: Select all

<?php


$loginface="<h4>Register<h4/>
			<form action='home.php' method='post'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
			</form>
			</div>
			<a href='index.php?$logout=true'>";

$logout;			
$logoutface="<br/><a href='index.php?logout=true'>Logout</a>"
;



//$logoutface="<form method=post action='index.php?logout=true'>
//<input type='submit' value='logout'>
//</form>";


if ($_SESSION['username'])
		{
		print "Hello ";
		echo  $_SESSION['username'];
		print "$logoutface";
		
			if ($_GET['logout']==true)
	{
  session_destroy();
  //$_GET['logout']=false;
  }
		
		}
	else 
	print "$loginface";		

//if ($_GET['logout']==true)
	//{
  //session_destroy();
  //$_GET['logout']=false;
  //}
	

	

	?>
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

I have taken the $ out btw
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help plz logout button without changing page

Post by Jonah Bron »

When you click "logout", what do you see in the URL? Please paste it here. Also, this part:

Code: Select all

print "$logoutface";
Fix both instances of those to this:

Code: Select all

print $logoutface;
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

http://www.hackking.co.uk/autoforward_index.php that is before clicking
http://www.hackking.co.uk/index.php?logout=true after one click

stays the same after second click but logs out im new to php only bin doing it one week thanx for this
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help plz logout button without changing page

Post by Jonah Bron »

Code: Select all

if ($_GET['logout']==true)
Instead of that, use this:

Code: Select all

if (isset($_GET['logout']))
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

Still doesnt work thanx for your ideas tho much appretiated

Code: Select all

<?php


$loginface="<h4>Register<h4/>
			<form action='home.php' method='post'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
			</form>
			</div>
			";

$logout;			
$logoutface="<br/><a href='index.php?logout=true'>Logout</a>"
;



//$logoutface="<form method=post action='index.php?logout=true'>
//<input type='submit' value='logout'>
//</form>";


if ($_SESSION['username'])
		{
		print "Hello ";
		echo  $_SESSION['username'];
		print $logoutface;
		
			if (isset($_GET['logout']))
				{
				session_destroy();
				//$_GET['logout']=false;
				}
		
		}
	else 
	print $loginface;		

//if ($_GET['logout']==true)
	//{
  //session_destroy();
  //$_GET['logout']=false;
  //}
	

	

	?>
tombruton69
Forum Newbie
Posts: 7
Joined: Wed Jan 19, 2011 1:06 pm

Re: Help plz logout button without changing page

Post by tombruton69 »

Solved it. It was ending the session just not displaying the correct information heres the code. Thanx for all of your help

Code: Select all

<?php



$loginface="<h4>Register<h4/>
			<form action='home.php' method='post'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
			</form>
			</div>
			";

$logout;			
$logoutface="<br/><a href='index.php?logout=true'>Logout</a>"
;
if (isset($_GET['logout']))
				{
				session_destroy();
				echo "successfuly logout";
				print $loginface;
				//$_GET['logout']=false;
				}

elseif ($_SESSION['username'])
		{
		print "Hello ";
		echo  $_SESSION['username'];
		print $logoutface;
		}
	else 
	print $loginface;
Post Reply