Session variable not being stored [duplicate] - 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.

Related

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

About PHPsession concept

shopping.php
<html>
<head>
<?php
if(isset($_POST['sub'])){
$a=$_POST['prod'];
$b=$_POST['qty'];
$_SESSION[$a]=$b;
}
?>
</head>
<body>
<form method="post" action="Billing.php">
Select any Product:
<select name="prod">
<option>Nokia</option>
<option>Reliance</option>
<option>Samsung</option>
<option>LG</option>
</select>
<br><br>
Quantity:<input name="qty"> <br><br>
<input type="submit" name="sub" value="ADD">
</form>
</body></html>
Billing.php
<html>
<head>
<?php
session_start();
echo session_id();
echo "<br>";
echo "selected products are:<br>";
// print_r($_SESSION);
foreach($_SESSION as $x=>$y){
echo "product is $x and Quantity is:$y<br>";
}
?>
</head> </html>
output
Warning: session_start() [function.session-start]: Cannot send session cookie -
headers already sent by (output started at E:\PHP programs\Billing.php:2) in E:\PHP programs\Billing.php on line 4
Warning: session_start() [function.session-start]: Cannot send session cache limiter -
headers already sent (output started at E:\PHP programs\Billing.php:2) in E:\PHP programs\Billing.php on line 4
Please, any one give the solution for the above Warning messages and how can I get the successful execution of a program?
You need to put session_start(); before any kind out output (at first line of file). session_start needs to send a cookie header which will fail if you've sent any output to user.
<?php
session_start();
?>
**shopping.php**
<html>
<head>
<?php
if(isset($_POST['sub'])){
SESSION must be at the top of your file. start code with the session_start();.
It looks like you have first declared <html> and then you have started session in billing.php just make changes as follows...
Billing.php
<?php session_start(); ?>
<html>
<head>
** Billing.php **
<?php
session_start();
?>
<html>
<head>
Remove any new lines, or any kind of output you might have before session_start in your php files that generating that error you posted.
May be you are trying to do something like this... check this out there are two files like you have created. The main thing you forgot to do is session_start() in your first file. So $_SESSION acted as an user created array variable rather than the session variable.
<?
//you always need to start session before using the session in php
session_start();
?>
<html>
<head>
<?php
if(isset($_POST['sub'])){
$a=$_POST['prod'];
$b=$_POST['qty'];
$_SESSION[$a]=$b;
// check if its being added in the session or not
print_r($_SESSION);
}
?>
</head>
<body>
<form method="post" action="">
Select any Product:
<select name="prod">
<option>Nokia</option>
<option>Reliance</option>
<option>Samsung</option>
<option>LG</option>
</select>
<br><br>
Quantity:<input name="qty"> <br><br>
<input type="submit" name="sub" value="ADD">
</form>
</body>
</html>
<?
//it is best way to close the session at the end of the file
session_close();
?>
<? session_start();?>
<html>
<head><title>Billing.php file</title></head>
<body>
<?php
echo session_id();
echo "<br>";
echo "selected products are:<br>";
print_r($_SESSION);
foreach($_SESSION as $x=>$y){
echo "product is $x and Quantity is:$y<br>";
}
?>
</body>
</html>

PHP Session variable being dropped halfway through script

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.

ECHO page1.php textarea values into page2.php

I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)

why is the value of textbox (this value is retrieved from the sessions using php) in html shown in IE

Okay the code is,
code in first.php
<?PHP
session_start();
include("script.php");
?>
<form action="script.php" method=POST>
<input type="text" value="<?PHP if(isset($_SESSION['info']['firstname'])){echo $_SESSION['info']['firstname']; }?>" name="firstname">
</form>
i have saved the file in php and the "script.php" page has all the code related to intialising the sessions in php like so
code in script.php
<? $info=new array();
$info['firstname]=$_POST['firstname'];
$info['lastname]=$_POST['lastname'];
session_start();
$_session['info']=$info;
?>
now when i open "first.php" in IE, the textbox is being filled with
<?PHP if(isset($_SESSION['info']['firstname'])){echo $_SESSION['info']['firstname']; }?>"
why is it so.thanks in advance.
Its look like you have a syntax error in script.php, it should be like this:
<?php
$info=new array();
$info['firstname']=$_POST['firstname'];
$info['lastname']=$_POST['lastname'];
session_start();
$_session['info'] = $info;
?>
Are you sure you access the page via a webserver?
In other words do you access it by http://youserver/first.php and not simply open the first.php file in IE file file:///path/first.php
You're likely seeing that because you've got <?PHP instead of <?php.

Categories