0

I would be grateful if someone could suggest what I need to do to get this to work.
I have copied two short scripts which should demo the use of $_session, but the read script does not work. I believe the save script is the culprit though. Here they are:
Program 1:

<?php 
session_start(); 
  // this sets variables in the session 
 $_SESSION['color']='red'; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round'; 
 print "Done";
 ?>

The Done is displayed when run.

Program 2:

<?php
session_start();
echo $_SESSION['color']; 
echo $_SESSION['size'];   
echo $_SESSION['shape'];
?>

When this is run I just get a blank screen

Thanks in advance for your time to reply to me.

Stephen

5
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by Rhamises
0

are cookies enabled? if so, try adding a link to the second page from then first one, something like:

<?php 
session_start(); 
  // this sets variables in the session 
 $_SESSION['color']='red'; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round'; 
 print "Done: <a href=\"second.php\">second page</a>";
 ?>

Have you got anything else on these pages?? Ensure that you place these scripts right at the top of your files.

Edited by diafol: n/a

0

No there is nothing else on these pages. I have investigated this as it is happening on some relatively complex database pages when filtering results based on criteria set in a session variable.
I have since found that if I run the script below before these examples or before my database pages, it all words fine. ?why???

<?php
session_start();
?>
<!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></title>
</head>
<body>
</body>
</html>
0

if they are the first things in each file, they should work. Do you have soemtyhing like...

first page:

<?php 
session_start(); 
 $_SESSION['color']='red'; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round';

//more php code??
?>
<!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></title>
</head>
<body>
<?php 
 print "Done: <a href=\"second.php\">second page</a>";
?>
</body>
</html>

second page:

<?php
session_start();
//more php code??
?>
<!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></title>
</head>
<body>
<?php 
echo $_SESSION['color']; 
echo $_SESSION['size'];   
echo $_SESSION['shape'];
?>
</body>
</html>

If this is how it's formed already, I don't think I can help you further.

0

Check if sessions are displaying on the same page or not?

<?php
session_start();
$_SESSION['color']='red';
$_SESSION['size']='small';
$_SESSION['shape']='round';
echo '<pre>';
print_r($_SESSION); 
?>
0

I would try and establish if the session was being started successfully to begin with:

if (!session_start()) {
  exit("Could not start session!");
}

If the session did not start, I would check the PHP configuration in php.ini to see how session data is set to be saved, and check that all folders etc are available and that sufficient rights, etc exist.

Also make sure you are in the same session, by checking that the session id is the same when each script runs.

I have had a similar problem when using AJAX, the AJAX session was not the same one as the HTML one, if that makes any sense.

Edited by NettSite: n/a

0

if they are the first things in each file, they should work. Do you have soemtyhing like...

first page:

<?php 
session_start(); 
 $_SESSION['color']='red'; 
 $_SESSION['size']='small'; 
 $_SESSION['shape']='round';

//more php code??
?>
<!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></title>
</head>
<body>
<?php 
 print "Done: <a href=\"second.php\">second page</a>";
?>
</body>
</html>

second page:

<?php
session_start();
//more php code??
?>
<!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></title>
</head>
<body>
<?php 
echo $_SESSION['color']; 
echo $_SESSION['size'];   
echo $_SESSION['shape'];
?>
</body>
</html>

If this is how it's formed already, I don't think I can help you further.

Yes and I even have copied your code above with no joy at all.

I have marked it solved as I need to get on, but I have only fiddled it at the moment by running the code mentioned above, before I load the pages using $_session to pass filter text from one prog to another.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.