About PHPsession concept - php

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>

Related

PHP - $_SESSION does not retain data after page refresh

I have the following problem and feel that the solution is simple but after 8 hours of trying and searching, I am giving up.
I have this simple page:
<?php
// Start the session
$lifetime=600;
session_set_cookie_params($lifetime);
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Change the Yoda!</title>
</head>
<body>
<?php
// Set session variables
$_SESSION["post-data"] = $_POST;
?>
<form action="yoda_is.php" method="POST">
YODA IS: <input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
Upon submit, it sends me to this page:
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Who is Yoda?</title>
</head>
<body>
<?php
// Echo session variables that were set on previous page
echo "YODA IS " . $_SESSION['post-data'] = $_POST['name'];
?>!
</body>
</html>
The value that you enter in the first page, is successfully being displayed on the second page.
However, once I close the browser window and revisit the second page, the value is no longer there and it returns an error.
My question is simple, what am I doing wrong / do I need to do in order for the value that I entered on the first page, to be there after I revisit the second page?
Thank you so much for your help and suggestions, in advanced.
KR
MD
On your first page remove this:
// Set session variables
$_SESSION["post-data"] = $_POST;
On your second page use this instead:
// If the user filled out the form, set our session variable to the new value
if(isset($_POST['name']))
{
$_SESSION['post-data'] = $_POST['name'];
}
// Echo session variable set above
echo "YODA IS " . $_SESSION['post-data'] . "!";

How to pass a Variable through a session

I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.
Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u

Warning: session_start(): Cannot send session, I have tried my all possible solution

I tried some solution from internet, I put session_start(); at first line, also try by using #ob_start(); Any other solution???
My code is here:
<?php
session_start();
require_once('page.inc');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>site 1</title>
<meta name="keywords" content="universe, blog theme, black, website template, templatemo" />
<meta name="description" content="About Universe, Our Company, free website template" />
<link href="templatemo_style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.6.3.min.js" ></script>
</head>
<body>
<?php
if(!isset($_SESSION['loggedinuser']))
{
if (isset($_POST['Signin']))
{
$username=$_REQUEST['username'];
if($username=='a')
$_SESSION['loggedinuser']=$username;
else
echo " the username or password you entered do not matchted ! ";
}
else
{
?>
<form method="post" action="index.php" >
<input type="text" name="username" id="username" >
<input name="Signin" type="submit" value="Signin" />
</form>
<?
}
}
else
//something else
?>
</body>
</html>
here the error is:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php:1) in /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php on line 2
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php:1) in /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php on line 2
Check whitespaces before <?php, ensure that the <?php is the first character, not tabbed or spaced.
I have tried your code and all works fine, I copied pasted it as is and the form is showing normal
even submitted the form twice once with the correct value and form disappeared and once with the wrong value and the message that it is a wrong value
if you have placed some white space before :
<?php
session_start();
....
?>
That might produce an error, nothing should come before the session_start() part, maybe you have note created the page: page.inc and that is doing the damage
This is the new code I tried, and these are step you can take:
I have checked it with opera, yes at first it was not showing, but with a heavy refresh it did show, try to do a no caching mechanism, by maybe sending it to page with a random id, this will clear the cache, the problem is not session in opera, the session is working, it is in caching,
Refresh your opera and you will, I took the address and copied it into another tab on opera, and the session showed, This is the complete copy on my version:
<?php
session_start();
require_once('page.inc');
?>
........
<?php
if(!isset($_SESSION['loggedinuser']))
{
if (isset($_POST['Signin']))
{
$username=$_REQUEST['username'];
if($username=='a')
$_SESSION['loggedinuser']=$username;
else
echo " the username or password you entered do not matchted ! ";
}
else
{
?>
<form method="post" action="index.php" >
......
</form>
<?
}
}
else if(isset($_SESSION['loggedinuser']))
{
$ses = $_SESSION['loggedinuser'];
echo "LINE 47 session is $ses ";
}
else
{
$ses2 = $_SESSION['loggedinuser'];
echo "LINE 51 session is $ses2 ";
}
?>
</body>
</html>
This produces:
Line 2 included a page LINE 47 session is a 56
page.inc
<?php
echo " Line 2 included a page ";
?>

Session variable not being stored [duplicate]

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.

session_start problems when run process in parallel

I need that both iframes forms work independent (async) but when i submit both forms at the same time the second iframe report "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\iframe2.php on line 2" (when the iframe1 query is a long process) or the second iframe return data AFTER the first process is complete (when the iframe1 query is a short process).
Now, i need keep the session for validate user login.
i need your help, my friends! thanks
index.php
<html>
<head>
<title></title>
</head>
<body>
<iframe src="iframe1.php" width="300" height="400"></iframe>
<iframe src="iframe2.php" width="300" height="400"></iframe>
</body>
</html>
iframe1.php (return query results)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
set_time_limit(120);
require_once("config.php"); //db conections
if($_POST)
{
//query (long process)
$data.= ""; // concatenated string with query results
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
ini:<input type="text" name="var1" value="" /><br />
fin:<input type="text" name="var2" value="" /><br />
<input type="submit" value="Send" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>
iframe2.php (only return 123456)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
if($_POST)
{
$data = "123456";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
<input type="text" name="inpt" />
<input type="submit" value="frame2" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>
Default PHP session have blocking file-access. That means as long as in your first script the session is still active, the second script's access to the session is blocked. PHP will wait until the session is accessible again.
The solution often is to keep the time-span short for the active period. Normally the sessions does not need to be active all the time.
You activate a session with session_start().
You deactivate a session with session_commit().
Locate the part of your script where you actually need an active session. Open it as late as possible (start) and close (commit) it as soon as possible.

Categories