How do I destroy a session by clicking a button? - php

This should be an easy one, if I explain it well.
I am trying to create a web page, where the session starts when the user enters a name, the name is displayed constantly, until the user presses the log out button; which should end the session. The closest I have gotten to is this:
<if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST["name"];
echo "<input type='submit' name='logout' method='logout' value='Log out'>";
echo "</form>";
echo "</h1>";
echo "</div>";
if(isset($_POST['logout']) && ($_POST['logout'] == "Log out")) {
session_destroy(); }
but it doesn't end the session and the name keeps being displayed constantly once entered. i.e. There is no way to end the session.
If you need any more explanation, or more of the code to understand, please let me know. Any help is much appreciated.

You can change session_destroy() by unset($_SESSION['name']) , like this:
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST["name"];
echo "<input type='submit' name='logout' method='logout' value='Log out'>";
echo "</form>";
echo "</h1>";
echo "</div>";
if(isset($_POST['logout']) && ($_POST['logout'] == "Log out")) {
unset($_SESSION['name']);
}
}

Use session_unset() to destroy all registered variables in the session and if you want unset a specific variable like name use unset($_SESSION['name'])
Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.

You can try to add a call to session_commit() just after session_destroy();

The code below handles the Logout hyperlink, so it creates the global variable logout and it unsets the session
Log out
<?php
if(isset($_GET['logout'])) {
session_unset();
}
?>

Related

How to avoid accessing session variables in php after logout?

I have a profile page in my website that welcomes the user with his/her name using session variable. After I unset this variable, the page can still access that name. I cannot properly erase the data.
I've tried to set it to null, session_unset and session_destroy
<?php #session_start(); ob_start(); ?>
//Some HTML code here
<?php
if( isset($_SESSION["user"]) && $_SESSION["login"]) {
echo '<div><p>welcome ' .$_SESSION["user"]. '!</p></div>';
echo
"<form action='' method='post'>
<input type='submit' name='use_button' value='Log out' />
</form>";
if(isset($_POST['use_button'])) {
$_SESSION["login"] = false;
unset($_SESSION["user"]);
session_unset();
echo "logout successful.";
echo '<script>window.location.href = "same-page.php";</script>';
}
}
else
echo 'no login data.';
?>
//Some HTML code here
<?php ob_end_flush(); ?>
I expected that after the redirect, the first if condition would not be satisfied and it gives the output 'no login data' but it still can access the session variables.
External php file:
<?php
session_start();
$_SESSION["user"] = '' ;
$_SESSION["login"] = false ;
echo '<script>window.location.href = "../profile.php";</script>';
?>
I think you can do this by destroying the session by using session_destroy() Method.
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
You can more about it from session_destroy()
here you are doing session destroy but you also need to do unset that particular variable from sessions array just like below before destroying session.
unset($_SESSION['user']);
Put this line immediately after your redirect line
echo '<script>window.location.href = "same-page.php";</script>'; // redirect
exit; // close current php script after redirect

how to pass session value other page if condition true in php

i have a $_sesstion['usermail']. i want to pass this value to next page.if condition match ($answer= $_SESSTION['usermail']);
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
header("Location:resetpass.php");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
i want to pass $_sesstion['usermail'] value on resetpass.php page.
I think your logic is wrong here. What exactly are you checking in the if statement. A session variable means you can use it on every page that has session_start(); on top.
Sessions by default pass to other pages.
Make sure you have start_session(); on top of the page you want to access the session variable.
So if $_SESSION['usermail'] is working on your current page, it'll work on your next as well with same data.
Get an idea from this exmple
First Page
<?php
session_start();
$_SESSION['name'] = "Adam";
?>
Second page
<?php
session_start();
echo $_SESSION['name'];
?>
You can use GET methods for sharing your session value to next page...
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$value_to_share=$_SESSION['usermail']; // You can share using GET
header("Location:resetpass.php?value=$value_to_share");
// receive this value at resetpass.php by $_GET['value']
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}

php SESSION gets destroyed after redirection

I have a page where I set a value to a SESSION but when I redirect to another page
ex. index.php that value I put to that SESSION doesn't exist anymore!
<?php
session_start();
// this is the page where I set a value to a SESSION called var!
$SESSION['var'] = "hello";
if(isset($SESSION['var'])){
echo "Yes it is";
header("location: test.php");
exit();
}
else {
echo "No it isnt";
}
?>
And this is the test.php where I get the SESSION undefined error!
<?php
session_start();
if(isset($SESSION['var'])){
echo "Yes it is";
}
else {
echo "No it isnt";
}
?>
Ass you can see, I put session_start(); in both pages but still nothing!
Any help would be much appriciated,
thank you!
P.S Im using XAMPP
To access session variables you need to access the $_SESSION. Change $SESSION to $_SESSION. Hope this helps.

global a $_SESSION variable

I'm trying to make a login session on PHP, but it appears that the $_SESSION['username'] dies inside the IF sentence (I thought $_SESSION where globals by default), and I cant echo it out of the IF
heres My code
if($name=="admin" && $password=="admin")
{
session_start();
$_SESSION['username'],$_SESSION['sesion'];
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
The last echo doesnt print its VALUE, So when I redirect it to another page, the page doesnt take the username value
I'm kind of new in this matter
So dont be so harsh on me :P
How can I do this??
Move session_start() to the top of the file:
// foo.php
<?php
session_start();
//....
if($name=="admin" && $password=="admin")
{
// $_SESSION['username'],$_SESSION['sesion']; // Remove this line
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
When You are using sessions in php You must start it with session_start() in every file/page You want to get or set session values, so just add this line to this file and that file You're redirecting to.
session_start() should be above the if statment. best to put it right below the ?php

Undefined index: username when using sessions

I'm working on a forum software right now, and I'm doing very well so far, it all works, but I can't get this to get away, I'm not really sure how.
I get Undefined index: username whenever I call
$_SESSION['username']
I start the session in a seperate login file, but get this error whenever calling it, I only get it when someone isn't logged in, so like if I'm logged in, it doesn't show it, only when I'm not logged in, how can I get rid of it?
Here's my whole code for the page:
<?php
session_start();
include_once("connect.php");
$find = "SELECT id,name,description FROM forums";
$run_find = mysql_query("$find");
if ($_SESSION['username']) {
print "Welcome, " . $_SESSION['username']. "!<br/>";
}
while($is = mysql_fetch_assoc ($run_find))
{
$id = $is['id'];
$name = $is['name'];
$des = $is['description'];
print "<div style='width:500px;background-color:#FFCCFF;'>";
print "Forum : <a href='topics.php?t=$id'>". $name . "</a><br/>" .$des . "<br/><hr>";
print "</div>";
}
if (!$_SESSION['username']) {
echo "
<form action='loginnext.php' method='post'>
Username: <input type='text' name='username'><br/>
Password: <input type='password' name='password'><br/>
<input type='submit' name='submit' value='Login'><br/></form>";
}
if ($_SESSION['username']) {
echo "<a href='logout.php'>Logout</a>";
}
?>
If the user is not logged in, the session variable will not be set, thus the error message.
Use if(isset($_SESSION['username'])) instead of if($_SESSION['username'])
if (!empty($_SESSION['username'])) is the construct you wanted. isset and empty can not throw notices.

Categories