View Full Version : Using PHP Session Variables
banjo
10-11-2006, 08:19 PM
I am trying to use PHP Session Variables on my website but it does not seem to work.
I have tried 2 methods as recommended in the manual. Below are extremely basic examples that do not work.
Method 1:
I include the following in the script of one page.
session_start();
$_SESSION['myvar'] = 5;
but when I then try to reference the session variable in another page, using the following, it does not exist.
session_start();
echo $_SESSION['myvar'];
Method 2 (Assuming register_globals is ON).
I include the following in the script of one page.
session_start();
$myvar = 5;
session_register('myvar');
but when I then try to reference the variable $myvar in another page, using the following, it does not exist.
session_start();
echo $myvar;
I am assuming the default settings on the digitalhost server should allow the use of session variables. Can anyone suggest a solution?
digital
10-11-2006, 10:41 PM
Hi Banjo
Firstly try creating a new empty file named php.ini
Place that file in the directory your working in.
Inside that file put your php directives such as
register_globals = 1 or On
or
register_globals = On or Off
and other similar commands and see how you go.
banjo
10-12-2006, 10:24 PM
I tried a cut down version of php.ini in the same directory as the script. No luck. I have removed it again.
The phpinfo() from the server indicates that register_globals is turned on anyway, although using the first method I mentioned with $_SESSION should not require it anyway.
The error log is not indicating any errors.
Perhaps if I could find out where it stores the session variables on the server, I could check and see if it is even being created.
Attached are the Session settings from the default servers php.ini file.
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
digital
10-12-2006, 11:01 PM
Banjo
Please post a ticket here http://www.digitalhost.net/support
takes seconds to register and a few minutes to post a ticket, include full details including your domain name, username and location of the issue.
Server details are not discussed on open forums and one our techs may be able to assist you.
Thanks.
banjo
10-15-2006, 04:34 PM
My problem has been solved. It did not require any changes on the server or to the php.ini file.
PHP code can usually be embedded within an HTML page almost anywhere
just by including it between an opening php tag "<?php" and a closing php tag "?>".
In fact all of my php scripts to date have run successfully this way, flipping between PHP and HTML.
However it seems that to use session variables reliably, it is necessary to include an opening php tag "<?php)" on the very first line of the page, followed by the session directive "session_start()". At least this seems to be the case with PHP ver 4.4.1
The following worked and set the variable $myvar so that it was available to scripts on other pages.
================================================
<?php <--------------------Note this is the first line on the page.
session_start();
?>
<html>
<head>
<title>Test Page 1</title>
</head>
<body>
<b>Test Page 1</b><br>
<?php
$_SESSION['myvar'] = 5;
echo 'myvar='.$myvar;
?>
<br>
<a href="test2.php">Go to Test Page 2</a>
</body>
</html>
=================================================
The following did not work reliably
=================================================
<------------------------Note the blank line at start of page
<?php
session_start();
?>
<html>
<head>
<title>Test Page 1</title>
</head>
<body>
<b>Test Page 1</b><br>
<?php
$_SESSION['myvar'] = 5;
echo 'myvar='.$myvar;
?>
<br>
<a href="test2.php">Go to Test Page 2</a>
</body>
</html>
==================================================
vBulletin v3.5.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.