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
tombruton69
Forum Newbie
Posts: 7 Joined: Wed Jan 19, 2011 1:06 pm
Post
by tombruton69 » Wed Jan 19, 2011 1:11 pm
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";
}
?>
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Wed Jan 19, 2011 1:35 pm
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
Post
by tombruton69 » Wed Jan 19, 2011 1:51 pm
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;
}
?>
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Wed Jan 19, 2011 1:57 pm
[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
Post
by tombruton69 » Wed Jan 19, 2011 2:06 pm
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
Post
by tombruton69 » Wed Jan 19, 2011 2:14 pm
I have taken the $ out btw
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Wed Jan 19, 2011 2:18 pm
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:
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Wed Jan 19, 2011 2:26 pm
Instead of that, use this:
tombruton69
Forum Newbie
Posts: 7 Joined: Wed Jan 19, 2011 1:06 pm
Post
by tombruton69 » Wed Jan 19, 2011 2:31 pm
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
Post
by tombruton69 » Wed Jan 19, 2011 2:47 pm
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;