Session variable gets lost between pages - php

I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!

You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];

in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>

Related

Storing Form Data as a Session Variable on Multiple Pages

I just wondering if its possible to store form data as Session on Multiple Pages?
I have 5 Pages on my Site, every page has an form with an input field on it.
I want save all Sessions and input values on all 5 Pages, and echo it on the last page.
I had saved the session_start(); in one file and include it on every page:
session_start.php
<?php
session_start();
?>
On the top of page before <!DOCTYPE html> i add it like this on every page:
Page 1:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['email'] = $_POST['email'];
}
?>
then in the Body:
<form action="" method"post">
<input type="text" name="email"/>
<input type="submit" name="submit" value="Enter" />
</form>
Page 2:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
?>
then in the Body again:
<form action="" method"post">
<input type="text" name="passwort"/>
<input type="submit" name="submit" value="Enter" />
</form>
I can echo the email Session on the Page 2 without Problems with:
<?php
echo $_POST["email"];
?>
But get an error on the page Undefined index: passwort if i do it on the same way as in Page 1.
And continue on the other 3 Pages, whats wrong with my Way?
With my code here i can save the Session only from the Page before and echo it on the next page.
Thanks for looking!
So long as you are successfully calling session_start(); at the start of each new page load, you will be carrying the previously store session data forward.
Yes, you will need to transfer the form-submission data from $_POST to the $_SESSION array each time.
After this snippet on page 1:
if (isset($_POST['email'])) {
$_SESSION['email'] = $_POST['email'];
}
Your session will contain:
$_SESSION = ["email" => "example#email.com"];
Then after this snippet on page 2:
if (isset($_POST['passwort'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
Your session will contain:
$_SESSION = [
"email" => "example#email.com",
"passwort" => "p#$$w()rt"
];
And so on, for the pages to follow.
When you want to check what is in your array, you can simply call var_export($_SESSION).
When you want to access particular elements, use their key.
E.g. echo $_SESSION['passwort'];

how to pass the post value to session to be use later?

i want to pass variable session value from post to 2nd page and so son but it doesnt work.
login page
<?php
session_start();
if ( isset($_POST['uname'] ) ) { $_SESSION['username'] = $_POST['uname']; }
?>
<html>
<body>
<form method="post" action="">
<input type="text" name="uname">
<input type="submit">
</form>
</body>
</html>
now i try to open my 2nd page call index.php to check if already assigment post value to session variable but nothing value pass.
<?php
session_start();
if ( isset($_SESSION['username'] ) ) { echo $_SESSION['username'];}
?>
You have to call session_start first in the second page to access the session variable.

Undefined index for Session variable

I am setting a session variable when a submit button is pressed like so:
<?php
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
However, in the page add.php when I do:
print_r($_SESSION['id'];
I get the following error:
Undefined index: id
I'm new to sessions so still trying to come to grips with them, but I thought the part where I do $_SESSION['id'] = $id; is where I define the index id to be the value of the $id variable? Could someone explain where I am going wrong?
UPDATE:
In case you're wondering if I am using session_start(): At the top of the page with the form, I include my header page which at the top contains require_once './init.php';. At the top of my add.php I just have the line require_once './init.php'; (I do not include the header file in add.php as this page will redirect as soon as it has executed its code.
In the init.php file is the following:
<?php
session_start();
require_once 'configurate.php'; //database info
?>
you should call session_start() before setting any values in $_SESSION
change your code like this
<?php
session_start();
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
I hope you have initialized your session via session_start before setting and retrieving the value from $_SESSION
if the value submitted via $_POST["submit"] compares to false (e.g. "" or 0) your condition if($submit) won't succeed, better use if (isset($_POST['submit'] instead

Session Variable not sending to next page

I am trying to set a session variable for the location selected in the dropdown menu when the user hits submit. The goal is to pass this variable to another php page so that I can display the drop down option they chose along with two values associated with it from a MYSQL table.
Below is a section of my code from my first php file. It shows the dropdown that is created from a list of values from the MYSQL database along with where I am trying to set the session variable for the selected option.
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
Below is my code from the second page where I call the session variables for display. As you can see I have input another variable for testing purposes. It is supposed to display "cat" and it does.
session_start();
echo $_SESSION['animal'];
echo $_SESSION['locations'];
It may also be important to note that locationsprocessing.php runs a redirect using header. But that is really all that file does.
Am I missing something? I have gone through countless different tuts and such and been staring at this code forever. I can not see why it wo
you need start session in every pages where you need to use session:
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
session_start();
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
This may be going wrong: When you submit your form in the first piece of code from the questions, you are sending the data entered in the form to /locationsprocessing.php. You say that all that file does is redirecting to another page. That means the data entered in the form is effectively lost.
I think what you want to do is to remove the form processing code from the first piece of code, so with only this remaining:
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
Then place that removed piece of code in /locationsprocessing.php, like this:
session_start();
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
header('Location: ' . $destination);
}
$destination would be the path for the page with the second piece of code in the question.

how to pass value from one php page to another using session

I can pass values form one page to another but I need to pass value like this,
Page 1:
Page4.php
Page3.php
I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
Edit..........
I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.
Its working now.
Thanks for the help.
Use something like this:
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
Any other PHP page:
<?php
session_start();
echo $_SESSION['myValue'];
?>
A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
Solution using just POST - no $_SESSION
page1.php
<form action="page2.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
page2.php
<?php
// this page outputs the contents of the textarea if posted
$textarea1 = ""; // set var to avoid errors
if(isset($_POST['textarea1'])){
$textarea1 = $_POST['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
Solution using $_SESSION and POST
page1.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
$textarea1 = "";
if(isset($_POST['textarea1'])){
$_SESSION['textarea1'] = $_POST['textarea1'];
}
?>
<form action="page1.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
<br /><br />
Go to page2
page2.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
// this page outputs the textarea1 from the session IF it exists
$textarea1 = ""; // set var to avoid errors
if(isset($_SESSION['textarea1'])){
$textarea1 = $_SESSION['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
WARNING!!! - This contains no validation!!!

Categories