Member Avatar for stephen_UK

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

Recommended Answers

All 7 Replies

Member Avatar for diafol

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.

Member Avatar for stephen_UK

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>
Member Avatar for diafol

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.

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); 
?>

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.

Member Avatar for stephen_UK

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.