[SOLVED] session variables not passing to other pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

[SOLVED] session variables not passing to other pages

Post by arukomp »

Hello,

I've finally finished my script and uploaded it to my server. Now what i noticed is that the session variables are not passing to other pages. Everything is written very good, the script works perfect on my computer with xampp.

Maybe someone knows what the problem could be? I had a look at phpinfo of the server, session support is enabled.

Also, i looked at session_start() function. It says "session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.". Now how do i pass session id? Maybe that would help?

Thanks a lot for your help
Last edited by arukomp on Thu Jan 04, 2007 4:31 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Try doing a phpinfo() and see what it says about sessions...

I've never had a problem using session_start() and the $_SESSION array.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

hmmm- You don't have to do anything to pass the session variables, they are just on. You access them on the next page.

ie:

Code: Select all

echo "$_SESSION['your var here']";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What does phpinfo() say about the following parameters
System
Server API
error_reporting
display_errors
session.use_cookies
session.save_path
?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This might also be helpful for us to know, so if you can, run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Everah wrote:This might also be helpful for us to know, so if you can, run the following in a new file and tell us the results please.
The results:

Code: Select all

PHP Version: 4.4.1
PHP OS: FreeBSD
Error Reporting: 2039 ((E_ALL | E_STRICT) ^ E_STRICT ^ E_NOTICE))
Register Globals: On
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Loaded Extensions:
   xslt             xmlrpc           xml              wddx          
   tokenizer        standard         sockets          session       
   pspell           posix            pcre             overload      
   mysql            ming             mhash            mcrypt        
   mbstring         imap             iconv            gettext       
   gd               ftp              exif             domxml        
   curl             ctype            calendar         bz2           
   bcmath           zlib             openssl          apache        
   Zend Optimizer
The server I'm running the script on is StartLogic.com

I've putted session_start() in the very first line of the script. I put $_GET['ref'] variable into $_SESSION['ref'] at the beginning of the script. At the end of the script i echo that $_SESSION['ref'] and it shows what it should show. Now when going to another page, $_GET['ref'] is not set, so i don't make any changes to $_SESSION['ref']. At the end of the second page, the script echoes empty space, like "ref: " and it should be like "ref: 3".
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

volka wrote:What does phpinfo() say about the following parameters
System
Server API
error_reporting
display_errors
session.use_cookies
session.save_path
?
Here it is:

http://www.abtrix.com/new2/test.php
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Can you try session_autostart=on in /etc/php.ini?? I guess it can help
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

I don't know if i can try... It's a shared server, so i can't change anything.

It seems I've wrote something wrong what didn't work well on the server. I changed some code.

I changed this code:

Code: Select all

$ref = $_GET['r'];
if ($ref != "") {
	$_SESSION['ref'] = $ref;
}
Into this:

Code: Select all

if (!empty($_GET['r'])) {
	$_SESSION['ref'] = $_GET['r'];
}
Now it's working very well :)
wildwobby
Forum Commoner
Posts: 66
Joined: Sat Jul 01, 2006 8:35 pm

Post by wildwobby »

arukomp wrote:I don't know if i can try... It's a shared server, so i can't change anything.

It seems I've wrote something wrong what didn't work well on the server. I changed some code.

I changed this code:

Code: Select all

$ref = $_GET['r'];
if ($ref != "") {
	$_SESSION['ref'] = $ref;
}
Into this:

Code: Select all

if (!empty($_GET['r'])) {
	$_SESSION['ref'] = $_GET['r'];
}
Now it's working very well :)
I've heard it was better practice to use isset() instead of !empty()
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

wildwobby wrote:I've heard it was better practice to use isset() instead of !empty()
They're not really the same thing. A variable can be set and be empty at the same time.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I always suggest checking whether a variable is set first, then checking if it is empty. But that is just me.
Post Reply