Page 1 of 1
How to access session vars in other page? (SOLVED)
Posted: Thu Jun 14, 2007 9:22 pm
by rozvinbm_jp
Code: Select all
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
echo '<br /><a href="page2.php">page2</a>';
?>
Code: Select all
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
if (isset($_SESSION['favcolor'])){
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
}else{
exit(0); //SESSION vars is not set.
}
echo '<br /><a href="page1.php">page1</a>';
?>
Scenarios:
1) load the page1.php
1.1) SESSION vars was created and initialized.
2) click the
page2 link
3) load the page2.php
4) echo SESSION vars.
Problem: SESSION vars is always unavailable. Therefore,
exit(0) is always executes.
Please help me to use SESSION vars and also the same problem with global vars. It always set to nothing the previously set the value in other pages then when the time I access the global vars, it always no value (
isset() function is always
false).
rozvin marchan
student
shizuoka-ken, fuji-shi, japan
Posted: Thu Jun 14, 2007 9:47 pm
by volka
please try
Code: Select all
<?php
// page1.php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
echo 'Welcome to page #1';
echo "<br />\n", session_name(), ' : ', session_id();
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
echo '<br /><a href="page2.php">page2</a>';
?>
Code: Select all
<?php
// page2.php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
echo 'Welcome to page #2<br />';
echo "<br />\n", session_name(), ' : ', session_id();
echo '<pre>'; print_r($_SESSION); echo "</pre>";
if (isset($_SESSION['favcolor'])){
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
}else{
exit(0); //SESSION vars is not set.
}
echo '<br /><a href="page1.php">page1</a>';
?>
Do you get any error messages or warnings?
Is the session_id printed on page #2 the same as on page #1?
Posted: Thu Jun 14, 2007 9:56 pm
by rozvinbm_jp
Warning: session_start() [function.session-start]: open(C:\Temp\sess_2l7qbhmqi7hivkc083sjadebq5, O_RDWR) failed: No such file or directory (2) in
C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\myeclipse\sample\page1.php on line 5
Welcome to page #1
PHPSESSID : 2l7qbhmqi7hivkc083sjadebq5
page2
Warning: Unknown: open(C:\Temp\sess_2l7qbhmqi7hivkc083sjadebq5, O_RDWR) failed: No such file or directory (2) in
Unknown on line
0
Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\Temp) in
Unknown on line
0
That is the error after loading of page1.php. Do i need to create the
Temp folder? I checked in the php.ini, its pointed in
C:\Temp.
Posted: Thu Jun 14, 2007 10:02 pm
by rozvinbm_jp
Welcome to page #1
PHPSESSID : g0p104hrherc8hl2evn46pon92
page2
This is the result after I created the
Temp folder.
Posted: Thu Jun 14, 2007 11:52 pm
by rozvinbm_jp
Welcome to page #2
PHPSESSID : ifkovrlkbqaggvg3qmfvqqop70
Array
(
)
This is the page when I click the page2 link.
I think it passes to FALSE during this condition if (isset($_SESSION['favcolor'])){.
Therefore, still no session vars is set. The problem still the same.
Please help me further..
This is my version:
PHPeclipse 3.1.2
PHP5.2.2
Apache2.2.4
Posted: Fri Jun 15, 2007 1:33 am
by suyeic
These two file works corectly on my web server. I think you must put these two files under the same domain.
Posted: Fri Jun 15, 2007 2:03 am
by rozvinbm_jp
Actually they are running but the
$_SESSION variables is always turns to unavailable. Therefore, all SESSIONS from
page1.php was became unset.
I've noticed that after I load the
page1.php, it creates a new session files in
c:\temp and the same with
page2.php but different session ID. I checked that it contains the information I need.
Do i need to pass the session ID of
page1.php in the URL when calling the
page2.php.
Code: Select all
echo '<br /><a href="page2.php?ses='.session_id().'">page2</a>';
Then, get the page1's session id via
$_GET from page2.php code and retrieve all the session file's contents using
session_decode.
If you have other way, please tech me.
Posted: Fri Jun 15, 2007 5:26 am
by volka
Your browser may have rejected the session cookie. If the session_id changes between page #1 and page #2 it means a completely new session has been started because php couldn't find a session_id in the request data.
Posted: Sun Jun 17, 2007 7:38 pm
by rozvinbm_jp
volka wrote:Your browser may have rejected the session cookie. If the session_id changes between page #1 and page #2 it means a completely new session has been started because php couldn't find a session_id in the request data.
In my case, If I call the other pages with session_start() on the first line, it will create a new session file and session id.
Do you mean that if I have session_start() on each pages, It must be only one session file and session id will be created?
Please confirm my conclusion on your message.
Posted: Mon Jun 18, 2007 8:44 pm
by rozvinbm_jp
Still with the given codes above... my $SESSION in other pages is unavailable.
Give me hints on this. onegaishimasu.
Posted: Mon Jun 18, 2007 8:58 pm
by feyd
Do you have these pages in a location where we can see them running, live?
Posted: Mon Jun 18, 2007 9:05 pm
by Ambush Commander
Probably not, considering they're on a Windoze machine.
Posted: Mon Jun 18, 2007 9:50 pm
by rozvinbm_jp
What do you mean?
I am using Windows XP Home Edition SP2.
I try to debug line by line using PHPeclipse.
Debugging:
1) after I click the page2 link.
2) breakpoints in the first line which is session_start() in page2.php. When I checked the Variables window the SESSION is not available.
3) after I passed thru the session_start() the SESSION was appeared in the Variables window but nothing.
4) of course the result of if (isset($_SESSION['favcolor'])){ was false.
maybe there is a configuration before using sessions.
Posted: Tue Jun 19, 2007 12:15 am
by rozvinbm_jp
What I need to do. I follow the instructions given in the PHP5 Documentation.. actually the code was copy and paste to my php code.. just to debug and know waht happening during execution.
Please assist me on this.
thanks a lot
Solution
Posted: Tue Jun 19, 2007 3:56 am
by rozvinbm_jp
rozvinbm_jp wrote:What do you mean?
I am using Windows XP Home Edition SP2.
I try to debug line by line using PHPeclipse.
Debugging:
1) after I click the page2 link.
2) breakpoints in the first line which is session_start() in page2.php. When I checked the Variables window the SESSION is not available.
3) after I passed thru the session_start() the SESSION was appeared in the Variables window but nothing.
4) of course the result of if (isset($_SESSION['favcolor'])){ was false.
maybe there is a configuration before using sessions.
Yeah I am right there is a php.ini configuration.
check the following items in your php.ini but it may be different on the filepath.
Code: Select all
[Session]
session.save_handler = files
session.save_path = "C:\Temp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = 0
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 = 5
Thanks to ALL!! This topic was solved.