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>
Related
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
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();
?>
I would like to maintain 3 $_Session variables after login. My login modal submits to my index.php page and this seems to be the only place I can access the session variables I set when the user logs in. How do I pass these variables to the next page the user visits? I know I can use hidden inputs in forms but what if the brows the site using the menu? I would like to store a users session variables in a session include file but I have the same issue passing the values of the variables from page to page.
-Mike
File a.php:
<?php
session_start();
$_SESSION['saveme'] = 'from file A';
?>
File b.php:
<?php
session_start();
echo $_SESSION['saveme']; // if you visited a.php previously, you will see "from file A"
?>
Setting a session variable in any file makes it available anywhere else.
You can store you values in session on one page(index in your case as you mentioned) then later on you can get those values on any page if session in started on that page. Session store those value till same session alive.
code to set values in session:
<?php
// Start the session
session_start();
?>
<?php
// Set session variables
$_SESSION["xyz"] = "xyz";
$_SESSION["abc"] = "abc";
echo "Session variables are set.";
?>
Code to get session values:
<?php
// Echo session variables that were set on previous page
echo "value of xyz is " . $_SESSION["xyz"] . ".<br>";
echo "value of abc is " . $_SESSION["abc"] . ".";
?>
The form of your modal
<form action="index.php" method="post">
Username <input type="text" name="username" />
Password <input type="password" name="password" />
</form>
Then you catch it in your index.php
<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) {
// Check if user exists and password matches
$_SESSION['username'] = $_POST['username'];
$_SESSION['logintime'] = time();
$_SESSION['something'] = 'else';
}
In any other page you can use the values like
<?php
session_start();
if (isset($_SESSION['username'])) {
echo 'Welcome ' . $_SESSION['username'];
}
All who have provided answers thank you. This overlooked detail was all on me and though I have been out of the dev game for a while I should have known better.
My hosting service by default makes all file permissions read/write only...to access session variables I changed to read/write/execute and was successful.
Again thanks!
I have two different page with below code. I trim whole code because more than 1000 lines and therefore trim what I did.
In the first page I remove session and fill with new array.
firstpage.php
session_start();
unset($_SESSION);
$_SESSION['new'] = 'test';
print_r($_SESSION); // Session Working fine here at the bottom of page.
secondpage.php
session_start();
print_r($_SESSION);
But in second page show empty array()! I don't have any idea why such this happen
First page header:
PHPSESSID e73jddq9fqhaeeav346h724js7 xxx.ttt.com 35B / Session
Second page header:
PHPSESSID e73jddq9fqhaeeav346h724js7 xxx.ttt.com 35B / Session
both is same.
The script works fine on windows PHP but in cpanel(linux base) doesn't work!
Please test this code :
firstpage.php
<?php
session_start();
$_SESSION['name'] = "john doe";
echo $_SESSION['name'];
?>
secondpage.php
<?php
session_start();
echo $_SESSION['name'];
?>
Login page:
<?php
session_start();
#echo session_id ();
$_SESSION["auth"]= "yes";
echo 'portal page link. Auth set to: {$_SESSION["auth"]}';
?>
Portal page:
<?php session_start();
echo "auth = {$_SESSION["auth"] } <BR />";
echo session_id ();
?>
The auth session is lost between the two pages somehow!
Edit
Here is a test url:
http://proserv01.services.lyris.com/NFPInsurance/UnsegmentedMemberReport/logintest.php
When trouble-shooting sessions, there are a few things I tend to do, but let's start with your code.
Here is an updated version of your page code so you actually see the value stored in $_SESSION['auth'] (your quotes were causing some trouble):
<?php
session_start();
$_SESSION["auth"] = "yes";
echo 'portal page link. Auth set to: ' . $_SESSION["auth"] . '';
?>
Here is the updated version of the portal page, which removes the additional space after the closing curly bracket:
<?php
session_start();
echo "auth = {$_SESSION["auth"]} <BR />";
?>
Now, if you don't see the auth with these revisions, you can try:
Changing the code in portal so it just dumps out the session so you can see what you've got: session_start(); var_dump($_SESSION);
Checking to make sure the error reporting is enabled, as PHP helps you identify many potential issues quite quickly (e.g., index doesn't exist, the headers were already sent, etc.): ini_set('display_errors','1'); error_reporting(E_ALL);
You can check your PHP config file (php.ini) to make sure that there are no settings causing session issues directly.
NOTE: For testing purposes only.
I am unsure as to what the expected results are, yet I will submit this as an answer with explanations set inside PHP comments.
Give this a try:
<?php
session_start();
$_SESSION["auth"]= "yes";
// will echo: portal page link. Auth set to: yes
echo 'portal page link. Auth set to: ' . $_SESSION["auth"] . '';
echo "<br>";
// will echo: auth = yes
echo "auth = {$_SESSION["auth"] } <BR />";
?>