Page 1 of 1
Help plz logout button without changing page
Posted: Wed Jan 19, 2011 1:11 pm
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";
}
?>
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 1:35 pm
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'].
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 1:51 pm
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;
}
?>
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 1:57 pm
by Jonah Bron
[text]<a href='index.php?$logout=true'>[/text]
Change it to this
[text]<a href='index.php?logout=true'>[/text]
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:06 pm
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;
//}
?>
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:14 pm
by tombruton69
I have taken the $ out btw
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:18 pm
by Jonah Bron
When you click "logout", what do you see in the URL? Please paste it here. Also, this part:
Fix both instances of those to this:
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:22 pm
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
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:26 pm
by Jonah Bron
Instead of that, use this:
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:31 pm
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;
//}
?>
Re: Help plz logout button without changing page
Posted: Wed Jan 19, 2011 2:47 pm
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;