I'm trying to save a session variable in one of my script and it seems to work fine for a portion of the script, however, it gets dropped in the second part of my code. I've echoed every line and can't figure out why this is happening. Also, this code was working just fine before, but ever since I switched servers, it seems to be malfunctioning. I don't think it's the session save path because the first part of the code works fine. Here is the basic outer structure of my code (I've removed all the irrelevant parts to save space):
<!--raw-->
<?php
session_start();
include 'config.php';
?>
<html>
<body>
<form id="driver_record" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
Enter Driver Number: <input type="text" name="driver" />
<input type="submit" value="Submit" name="user_submit" />
</form>
<?php
if(isset($_POST['user_submit']))
{
if(username_exists($_POST['driver'])){
$ulog = $_POST['driver'];
$_SESSION['user_id'] = $ulog;
echo $_SESSION['user_id']; //Session echos fine here
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
?>
<form id="expenses_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<input type="submit" value="Approve" name="record_approve" />
<?php
}
}
?>
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
}
//unset($_SESSION['user_id']);
//session_destroy();
?>
</body>
</html>
<!--/raw-->
EDIT: I have commented out the unset statement, and also tried putting them inside the if statement, with no change to the behavior of the code.
Everytime the code is run you are calling the unset($_SESSION['user_id']); this is because it is not within your IF statement, you have a curly brace in the wrong place.
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
unset($_SESSION['user_id']);
session_destroy();
} //moved curly brace here
?>
EDIT: The code runs fine for me, I had to comment out the include line and the function used, the fault must be within the config.php. Do you have a session_start() at the top of that file and no unsets used ?
Here is a simple session script, are you having problems with this ?
If so then there is a problem with php.
<?php
session_start();
$_SESSION['testing'] = "boo";
echo "Session 1=" . $_SESSION['testing'];
?>
<form action="session1.php" method="post">
<input type="text" name="input"
<input type="submit" />
</form>
<pre>
<?php
if (isset($_POST['input']))
{
print_r ($_SESSION);
print_r ($_POST);
}
?>
It seems that neither I or anyone else here can find a logical solution as to what's happening. I've decided to simply remove the session variables and include a 'hidden' input field that just sends the variable through a POST variable. This seems to solve all my problems without having to worry about the behavior of the session variables.
You set the session value only when the first form is submitted (isset($_POST['user_submit'])). When the script is processing the second form submission (isset($_POST['record_approve'])), no session value is set. Hence no output.
You do realize that, even if the script is in one PHP file, it will be triggered by two mutually exclusive form submissions, right? So the session value will be available only after isset($_POST['user_submit']. So if you submit the second form after submitting the first one, you will have a session variable 'user_id', provided you have commented out the lines that unset and destroy the session.
Hope it makes sense.
Related
I have this page I am working on which is a sort of a game.
From page to page I am passing the user name like this
index:
<form method="GET" action="start.php">
<label> Name: </label>
<input type="text" name="username"><br>
<input type="submit">
</form>
Where I retrieve the name on the second page like this:
<?php
session_start();
$_SESSION['username'] = $_GET['username'];
?>
-
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Welcome: " . $_SESSION['username'];
}
else{
echo "Name is unknown";
}
?>
And also on the third:
<p>
<?php
session_start();
echo $_SESSION['username']
?>
</p>
And this code is working just fine. Now I was making an if statement which, when ever you don't enter a name, you won't continue to the next page. I added this code to the first page and this is working for the first page.
<?php
session_start();
$_SESSION['username'] = $_GET['username'];
if($_SESSION['username'] != "")
{
header("Location: start.php");
}
?>
So after adding this, you won't go further unless you do enter a name. But by doing this for a reason I don't know yet and couldn't find, the $_SESSION['username'] = $_GET['username']; isn't working and the names are not passed through
This is the link if you like to play: http://i333180.iris.fhict.nl/site
(not finished yet)
These are your problems:
1- You don't need to use more than one session_start() in a file.
2- you have closed php tag in the second code and then countinued the PHP coding.
3- before session_start(), it must not send any header or echo anything. In the 3rd code, you have echoed tag before session_start().
4- same as session_start(), you must not send any header or echo anything before calling header function. Be sure in the 4th page, you do not have anything echoed before header().
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
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');
}
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: Headers already sent” in PHP
Why does this not work? I expect to see whatever is input into the text box in first.php displayed back out in test.php.
<html>
<body>
<form action="test.php">
Test: <input type="text" name="test" />
<br> <input type="submit" name="submit" />
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
</body>
</html>
Here is test.php
<?php
session_start();
$check = $_SESSION['test'];
echo $check;
?>
session_start() must be called before outputing anything(even not white space) to the browser.
you are starting session after html start session on the very first line of page like
<?php
session_start();
print_r($_SESSION);
if(isset($_POST['submit'])){
$_SESSION['test'] = $_POST['test'];
}
?>
<html>
<body>
<form method="post" action="addimageprocess.php">
Test: <input type="text" name="test" />
<br>
<input type="submit" name="submit" />
</form>
</body>
</html>
session_start() should be set before any headers are sent (i.e. at the very top of the page)
The PHP code is executed before the actual POST is done, because PHP is a server-side language. At that point, there is actually no $_POST array.
This code, from your eg.
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
The $_POST is posted (or exists), to the test.php page.
The PHP in your main script is not doing what you want it to do, in fact is just saving junk and or null in best of the cases. You need to inverse the PHP scripts, put
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
in test.php and
<?php
session_start();
$check = $_SESSION['test'];
echo $check;
?>
in you main php
If you are using a webserver, and you do have
<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>
but it still isn't storing the session then you will need to check the saved_path
<?php
phpinfo():
?>
check under the session header to see if the session:save_path is set and make sure that it exists on the server. if its a webserver then you will have to contact your host to set up the save path for you.
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!!!