Session variables not working? (PHP)
In profile.php
you have to call session_start();
before using $_SESSION
. session_start()
doesn't just start a new session, but will also continue an existing session (it will 'start' the session handling functionality, if you will). Without calling it, you cannot use $_SESSION
.
PHP session variable not working across 2 pages
Please try executing following code snippet
<?php session_start();?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$name = $_POST['name'];
$_SESSION['name']=$name;
}
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Actually in your code snippet value for $_SESSION['name'] was not set .so I have defined value for session variable with posted value from HTML form
Session in PHP not working. Why?
session is not saved because in if condition you are checking the variable submit instead of $_GET['submit']
and also you didn't specify name of submit button
<input type="submit" name="submit" value="sign in">
<?php
if(isset($_GET['submit'])) {
$_SESSION["user"] = $_GET["user"];
$_SESSION["password"] = $_GET["password"];
}
?>
error of undefined variable is due to $_SESSION['user'] & ['password ']
is not set because $submit
is NULL variable and also you missed to close square bracket in password
$_SESSION variables not working, not working in other pages
You must start the Session before using it. It doesn't seem that way from your code...
Try putting the code below at the very top of each of your Files and see how it goes:
<?php
// THIS SHOULD BE THE VERY FIRST LINES OF CODE IN YOUR SCRIPTS
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
php session variables on multiple pages not working
Tried it locally using the following code:
1 test.php
`<?php
//page 1
session_start();
$user="dvjnvki";
$_SESSION['user_name_loggedin'] = $user;
header("Location: b.php");
?>`
2 b.php
<?php
//page2
session_start();
if(isset($_SESSION['user_name_loggedin'])){
echo $_SESSION['user_name_loggedin'];
}else{
echo 'not set<br>';
}
?>
and it does work perfectly. So the issue with your code might be that you might not be getting a value for the variable called $user. Try to echo that first and see if you get an output.
PHP Session Variables Not Working After Redirect
You should learn more about sessions to avoid making mistakes and not leaving your codes vulnerable!
Know that to work with sessions, you must start them right at the beginning of each script
Also, after you create your session, you don't need to use the 'echo' command and right after redirecting to the success page, in fact, it is on the success page that you should work with the 'echo' command, and create some variables to store the value of those sessions, to make it easier to work with, and to make the code cleaner!
Please try it:
Signin
<?php
session_start();
//Start the session in the top of the script
if (password_verify($rawpassword, $row["passwordHash"])) {
$_SESSION["email"] = $email;
$_SESSION["fname"] = $row["firstName"];
header("Location: home.php");
exit();
} else {
header("Location: signin.php?addlComment=3True");
exit();
}
Home
<?php
session_start();
session_regenerate_id(true); //It can help you to protect against attacks, try to learn it!
$email = $_SESSION['email'];
$first_name = $_SESSION['fname'];
//If the user try to access the page without make login, then redirect to the signin page
if(!email || !first_name)
{
header("Location: signin.php");
exit();
}
//Test the sessions variables
echo "Welcome, you're logged in! I know your first name is: {$first_name}";
Related Topics
Jquery Validate Remote Method Usage to Check If Username Already Exists
How to Get Parameters from a Url String
How to Replace Certain Parts of My String
Force Ssl/Https Using .Htaccess and Mod_Rewrite
How to Validate an Email in PHP
How to Strip All Spaces Out of a String in PHP
Make All Words Lowercase and the First Letter of Each Word Uppercase
Converting String to Date and Datetime
How to Encrypt and Decrypt a PHP String
How to Convert Json String to Array
MySQL and PHP: Utf-8 With Cyrillic Characters
Best Way to Get Result Count Before Limit Was Applied
Laravel Requires the Mcrypt PHP Extension