Could somebody please direct me? Why the code is not working?

Let's say I have two scripts: mypage.php and mypage2.php.

mypage.php:

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

Works as expected, outputs "Done".

mypage2.php:

<?php // this starts the session 
session_start(); 
// echo variable from the session, we set this on our other page 
echo "Our color value is ".$_SESSION['color']; 
echo "Our size value is ".$_SESSION['size']; 
echo "Our shape value is ".$_SESSION['shape']; 
?>

Doesn't output the values of the variables. Outputs just this:
"Our color value is Our size value is Our shape value is"

What am I doing wrong?

Thank you!

It should work.

Here is what I found in the php.ini file:

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.

Could that cause the problem? And how do I fix it?

yeah dude it should work, maybe you got problem on retrieving your stored session, try inspecting your php.ini where did you set it or you can explicitly define it using session_save_path('path/path'), put it before the session_start. hope that helps ^_^

It should work.

but its better to check the php.ini

I did check the php.ini and found this:

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.

The problem is I'm not sure what and how I have to change.

Never encounter something like that. Have you try another browser? Or use a PHP package such as XAMPP

this is my working php.ini

session.save_handler = files
session.save_path = "D:\xampp\tmp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor     = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 4
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="

The script is fine. I copy and pasted and works fine...by any chance sessions cookies not enable while you performing the test?

The script is fine. I copy and pasted and works fine...by any chance sessions cookies not enable while you performing the test?

How do you enable session cookies?
Also, my php.ini says that Windows users need to make a change to session.save_handler variable.
I'm new to this and don't know what to do.

The script is fine. I copy and pasted and works fine...by any chance sessions cookies not enable while you performing the test?

This is what I've found in my php.ini:

session.use_cookies = 1

Does this mean the session cookies are enabled?

Hi and I'm back. Now as for the solution sessions do use cookies for user identification. So if cookies are disabled then sessions are disabled. However there is a work around without changing the php.ini file at all and is as simple as changing the url of pages with sessions. Below is a example of how to setup the link url for each page in your website.

<?php session_start();

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Now as you see the defined variable 'SID' is inserted into the url. Then sessions will find this variable use it for user identification instead of cookies and will then be able to use sessions without cookies.

Hope that helps.

the way I do it is,

echo 'The color value is ';
echo $_SESSION['whatever sesh here'];
echo 'The Length Value is ';
echo $_SESSION['what ever sesh here'];
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.