Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have create session in header.php but when I change my page session get destroyed automatically .
session start this code i have place at very top of header.php
session_start();
php code for session
<?php $_SESSION['lang'] = $_GET['herbew'];
if(isset($_SESSION['lang'])){
echo $_SESSION['lang']; } ?>
HTML where I give option to user to change language
<form action="<?php echo $_SERVER['REQUEST_URI']?>" method="get">
<div class="lang">
<span class="english"><input type="hidden" name="english" value="english">
<input type="submit" name="submitsd" value="eng" ></span>
<span class="herbew"><input type="hidden" name="herbew" value="herbew">
<input type="submit" name="submit" value="her" ></span>
</div>
</form>
A session must be started inorder to access the session variables.
At the top of the page add:
session_start();
This will start a session. it's also required to have session_start(); on every page that uses the session array.
If you are looking to add data to the session array. You will need to do this after user presses the submit button. Ie,
Form > Redirects to AddSess.php > Adds required values to session > Redirects to the correct page
Ie:
<form action="AddSess.php" method="POST">
<input type="text" name="test">
<input type="submit" name="submit">
</form>
and on AddSess.php
session_start();
if (isset($_POST['submit'])){
$_SESSION[] = $_POST['test'];
header ("Location: index.php");
}
The above is a very basic example on how to append data to a session
Add variable in session variable in PHP
You are overwriting your session variable everytime the page is called. if $_GET['herbew'] is empty, so will be $_SESSION['lang']
Check $_GET before you assign it:
if (isset($_GET['herbew'])) {
$_SESSION['lang'] = $_GET['herbew'];
}
if(isset($_SESSION['lang'])) {
echo $_SESSION['lang'];
}
Related
There are similar questions related to the topic but none of them have solved my problem. Its kind of weird but my $_SESSION is working on the same page but not on any other page. If I put isset($_POST['submit') the condition doesn't satisfy and without it the $_SESSION remains null.
This is my code.
This is the login page.
<!-- Login.php -->
<?php
session_start();
?>
<html>
<body>
<form method="post" action="profile.php">
<fieldset>
<legend>
Login
</legend>
<label> User ID :</label> <input type="text" placeholder="Username" name="user"><br>
<label> Password :</label> <input type="password" placeholder="Password" name="password">
<input type="submit" name="submit" value="Login">
</fieldset>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
?>
This is where I want my session variable to appear.
<!-- profile.php -->
<?php
session_start();
echo "Session user is ".$_SESSION['USER']."<br>";
unset($_SESSION['USER']);
unset($_SESSION['PASS']);
session_unset();
session_destroy();
?>
This is what I have tried :
Changing form method to GET.
Using $_REQUEST and $_GET.
Using $_SESSION on the same page. It works on the same page.
Checking session id. The session on the other pages are present but values are either null or empty.
Running the code without isset(). In that case all the session variables remain NULL.
$_POST['submit'] and the rest of the post parameters are not available in Login.php
They are available only in profile.php because your form action points to it.
You may move the following code after the session_start() in profile.php.
if(isset($_POST['submit'])){
$_SESSION['USER']= $_POST['user'];
$_SESSION['PASS']=$_POST['password'];
}
Keep in mind that you unset the session values in the end of profile.php
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'];
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.
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!!!
Suppose that I have also coded a similar login form then issue the session by name via $_SESSION['name'] as follows
session_start();
if(!isset($_SESSION['name'])){
header("Location: login.php");
}
then right on the same file (display.php) I also display a form to post a message to the administrator to tell him about how I feel such as
<td>
<form action="tellhim.php" method="POST">
Title:<input type="text" col="30" name="comment_title"/><br/>
Your feeling:<br/><textarea name="comment_content" col="10"></textarea><br/>
<input type="hidden" name="postfeeling" value="TRUE"/>
<input type="submit" value="Submit"/>
</form>
</td>
that means, right after I click the button to submit my feeling I will be directed to tellhim.php. The problem then is that the session seems invalid right after the page is reloaded. Could someone help me out please ?
You should exit(); after header()
file tellhim.php needs that also:
session_start();
AND the session_id has somehow to be added to tellhim.php, automagically like this:
ini_set('session.use_cookies', 1);
ini_set('session.use_trans_sid', TRUE);
ini_set('url_rewriter.tags', 'a=href,area=href,script=src,link=href,frame=src,input=src,form=fakeentry,form=post,form=action');
session_start();
You need to use session start() before all the files that you want to use
//login.php after login redirect to display.php
session_start();
//set session variable
$_SESSION['name'] = 'xxx';
//display.php
session_start();
if(!isset($_SESSION['name'])){
header("Location: login.php");exit;
}
{rest of the form code goes here}