Make $_SESSION["logged_user"]="" instead of Destroying Sessions in Sign-out page - php

I need to display different content for logging users and others. So I make that part as following code.
<?php
if($_SESSION["logged_user"]=="")
{
?>
<div id="test">Display for un-logged users</div>
<?php
}
else
{
?>
<div id="test>Display for Logged Users</div>
<?php
}
?>
I can't destroy all sessions in sign-out page because of I use "$_SESSION["logged_user"]" in every page.
So I make sign-out page like this.
<?php
session_start();
$_SESSION["logged_user"]="";
?>
I just empty the $_SESSION["logged_user"] instead of destroying session.
Is it a bad idea? If it is a bad idea what is the solution for me?

<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>

Related

My sessions aren't working correctly

I have the following code
<?php
if($_SESSION['loggedin']){
echo '<li id="login-btn">Logout</li>';
}
else{
echo '<li id="login-btn">Login</li>';
}
?>
This is inside of the HTML for my Navbar. I want it to where if they are logged in, it will show "Logout", if they aren't logged in, it'll show "Login", (self explanatory)
I have this in my login.php
$loggedin = "";
$_SESSION['loggedin'] = true;
For some reason, no matter what I do, my navbar keeps displaying "Login"? Help please, thank you!
Session are global variables in php...
Session variables are not passed individually to each new page,
instead they are retrieved from the session we open at the beginning
of each page (session_start()).
if you want to access it on different page... you have to add
<?php
session_start();
?>
at the begining .... even in your login.php page

Session array resets on submit?

I have test.php:
<html>
<?php
if (!isset($_SESSION))
{
echo 'session is not yet set';
session_start();
$_SESSION['comments'] = array();
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="text" name="comment">
Click the button!
<input type="submit"/>
</form>
<?php
array_push($_SESSION['comments'], $_GET['comment']);
echo 'Current array count: ' . count($_SESSION['comments']) . '<br>';
foreach($_SESSION['comments'] as $comm)
{
echo $comm.'<br>';
}
?>
</html>
What should happen is that any text entered in the textbox should be pushed to the session array, but every time I click on submit, what is printed is the text I just entered instead of appending/storing the previous entries. I only get 1 as the array count all the time.
Every time I hit submit, session is not yet set always shows at the top of the page even when I have the if condition.
I can't seem to find what's wrong?
You need to do this:
<?php
session_start();
if (!isset($_SESSION))
{
?>
Otherwise you have header output from the HTML tag and the session can't start properly.
You should start the session at the top of the page always.
session_start(); //start the session
Then we can check session is exist or not.
if(!isset($_SESSION['user'])){
// your code
}else{
// your code
}
session_start() does not just start a NEW session. If the client already has a session it resumes that session. PHP Manual
If you are using sessions, just have session_start(); at the top of the page and PHP will do the lifting of starting or resuming a session.

Php session refresh to see user's settings change

I've got an issue, I've looked everywhere and can't find an answer.
On one of my pages I have it set so users can change there firstname/lastname etc, once the change is done, they can't see there firstname change, the only way they can at the moment is to logout then login again.
I've tried session refresh, but that doesn't help, although it does change the session ID.
If someone could link me a tutorial or even post a snippet of code that does that, it would be great! Thank you. My current script for displaying the user's data is via session.
<?php
error_reporting(E_ERROR);
?>
<?php
session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or
empty($_SESSION["user"])
)
// redirect to index page if not superuser
header('Location: index.php');
mkdir('users/'.$_SESSION["user"]["id"])
?>
<?php
$old_sessionid = session_id();
session_regenerate_id();
$new_sessionid = session_id();
echo "Old Session: $old_sessionid<br />";
echo "New Session: $new_sessionid<br />";
print_r($_SESSION);
?>
<h3 align="left"><?= $_SESSION["user"]["firstname"] ?></h3>
<h3 align="left"><?= $_SESSION["user"]["id"] ?></h3>

how to logout without logout.php

The php code below is login_successful.php which is obtained after user logs in, in this page i want to display his 'username' and a logout link
<html>
<head>
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:home.html");
}
?>
</head>
<body>
Welcome $myusername //here i want to display logged in user's name
Login Successful
</body>
</html>
how should i put logout link in this page without using another logout.php file.
Why use another page for logout? Do it like this
<?php
if(isset($_POST['logout'])) {
//Unset cookies and other things you want to
session_destroy();
header('Location: login.php'); //Dont forget to redirect
exit;
}
?>
<form method="POST">
<input type="submit" name="logout" />
</form>
You have to check wheter session has his username and then display, something like:
session_start();
if(isset($_SESSION['username'])){
echo "Hello, " . $_SESSION['username']);
echo "Logout"
}
You can always call session_destroy() to (guess what) destroy your sessions! From the manual:
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.
More important than use session_destroy() is to make sure you reseted the cookie (if any used) by setting it's time one hour back: time() - 3600, like:
setcookie ("YourCookieName", "", time() - 3600);

I can't echo session username, am I storing session username and id correctly?

I am setting up a login form.
Expected Result:
Echo session username on page after successful login.
Actual Result:
Login is successful. Session username does not echo. Appears as though session username either does not exist or it is not persisting to the next page.
Is there something wrong with the code below?
LOGIN.PHP
...
session_start();
if (mysql_num_rows($result) ==1)
{
session_regenerate_id();
$row = mysql_fetch_assoc($result);
$profileid = $row['userid'];
$profile = $row['username'];
//Set session
$_SESSION['profileid'] = $profileid;
//Put name in session
$_SESSION['profile'] = $profile;
//Close session writing
session_write_close();
//Redirect to user's page
header("location: index.php?msg=userpage");
exit();
}
...
INDEX.PHP
...
<?php
session_start();
if($_GET['msg']=="userpage")
{
echo $_SESSION['profile'];
}
...
Edited:
Put session_start in php tags.
Changed HTML to INDEX.PHP.
"If" brace closed.
Changed while to if in LOGIN.PHP.
Changed username to userpage
You don't need to be opening/closing sessions, it's not worth the extra lines of code. I also don't know why you're regenerating the session ID.
But, one thing is your HTML file is badly constructed, and it almost looks like the session_start() isn't inside any PHP tags, so it's not even being treated as code.
first of all your HTML is yet PHP as it involves PHP tags only.
Replace while with if coz you only want to set the $_SESSION variables once.
And for the last part what you are looking for is this
<?php
session_start(); //at the beginning of your script
if($_GET['msg']=="username")
{
echo $_SESSION['profile'];
}
?>
Make sure you eliminate all the whitespaces before the opening of your first <?php tag on your script as that gives potential header errors.
close the if loop in html file
EDITED:
I did this simple code in my page and as per session concept is concerened The code is working fine...make corrections accordingly
p1.php
<?php
session_start();
//Put name in session
$_SESSION['profile'] = "Pranav";
//Close session writing
//Redirect to user's page
header("location: p2.php?msg=userpage");
exit();
?>
p2.php
<?php
session_start();
if($_GET['msg']=="userpage")
{
echo $_SESSION['profile'];
}
?>
FOR NEW SESSION ID
USE THIS
$a = session_id();

Categories