How to use store and use session variables across pages?
Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.
Reusing session variables across multiple pages
Am I missing quotes?
Yes. You say you're accessing them like this:
$id = $_SESSION[id];
That should be this:
$id = $_SESSION['id'];
But even more to the point, why do you need to use session here at all? The way you describe the situation is:
- User makes a request with query string values in the link.
- In the response you forward the user to another page (presumably using the location header?).
- On the last page the values need to be present.
If they're query string values, keep them as query string values in the redirect. So where you may have something like this:
header('Location: somePage.php');
you can include the values:
header('Location: somePage.php?name=' . $name');
and so on for the remainder of the values, just like you do when building the original link for the page which performs the redirect.
Also, while you don't show your data access, you do show your query which appears to be vulnerable to SQL Injection attacks. Ultimately the values you're using are coming from user input (query string) so you shouldn't directly concatenate them into SQL queries.
PHP: How to carry over session variables between 3 pages?
page1
<?php
session_start();
// next 2 lines do NOTHING remove them
// as you have not yet loaded any values into $q and $s
//$_GET['q'] = $q;
//$_GET['s'] = $s;
?>
<form action="search.php" method="get">
<input name="q" maxlength="8" type="text" placeholder="License Plate" id="textbox" required />
<select name="s" id="s" required aria-required="true">
<option value="" disabled selected>CHOOSE STATE</option>
<option value="AL">ALABAMA</option>
<option value="AK">ALASKA</option>
<option value="AZ">ARIZONA</option>
<option value="AR">ARKANSAS</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
etc...
</select>
<input type="submit" value="SEARCH" id="submitbtn"></form>
Page 2 - Search - receives data from previous form
- Contains lots of unecessary <?php...?>
- Previous form uses method="get"
so data will arrive in the $_GET
array not the $_POST array
<?php
session_start();
//$q = $_POST['q'];
//$s = $_POST['s'];
// But this is silly as you have not yet tested these values exist
// but you do that in the next lines
//$q = $_GET['q'];
//$s = $_GET['s'];
$dir = 'states';
$s = (isset($_GET['s']))? strtolower($_POST['s']) : '';
$q = (isset($_GET['q']))? strtoupper($_POST['q']) : '';
$res = opendir($dir);
// Now if you want to pass the values of `q` and `s` on to the next form
// they now need to be added to the session
$_SESSION['q'] = $q;
$_SESSION['s'] = $s;
while(false!== ($file = readdir($res))) {
if(strpos(strtoupper($file),$q)!== false &&!in_array($file)) {
echo "<a href='$dir/$s/$q.htm'>$file</a>";
}
}
closedir($res);
echo $htmlHeader;
while($stuff){
echo $stuff;
}
echo "<script>
window.location = 'http://www.somesite.com/$dir/$s/$q.htm';
</script>";
// added missing semi colon ^
?>
Page 3 (404 page for catch all that are not in the system):
Now the data will be available in the SESSION, when you get to this page.
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.
Related Topics
Seo Friendly Url Results in CSS Img and Js Not Working
How to Prevent Form Resubmission When Page Is Refreshed (F5/Ctrl+R)
How to Check If an Email Address Exists Without Sending an Email
Check If PHP Session Has Already Started
PHP MySQLi_Connect: Authentication Method Unknown to the Client [Caching_Sha2_Password]
"Fatal Error: Cannot Redeclare ≪Function≫"
How to Encrypt/Decrypt Data in PHP
Highlight the Difference Between Two Strings in PHP
How to Create Friendly Url in PHP
Simplest PHP Example For Retrieving User_Timeline With Twitter API Version 1.1
How to Get File_Get_Contents() to Work With Https
Convert from MySQL Datetime to Another Format With PHP
How to Get Parameters from a Url String
What's Wrong With Using $_Request[]
Sending Email With Gmail Smtp With Codeigniter Email Library