Page 1 of 2
sessions problem
Posted: Sun Aug 21, 2005 1:57 am
by laticca8
I used this simple code to see if my sessions would work, but i see that they don't.
Code: Select all
session_start(); session_register('count');
$count++;
echo "<P>You've been here $count times.</p>";
i reloaded several times, but the number remained 1. i also tested some other pre-written script which resulted in simply a blank page.
i have checked the ini file and have these session settings:
register_globals = Off
session.save_handler = files
session.save_path = ".....\sessions"
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 1
session.cookie_lifetime = 0
session.cookie_path = /
session.serialize_handler = php
i use an IIS server. any thoughts?
Posted: Sun Aug 21, 2005 2:45 am
by Nucleus
well...
when your using a session you dont have to officially register it its enough to do:
as above, when your using a session variable, you just do:
and offcourse start the session with session_start()...
Posted: Sun Aug 21, 2005 2:53 am
by laticca8
okay, well here's some script that didn't start out like that and resulted in a blank page. here is the first page called sessiontest1.php:
Code: Select all
session_start();
?>
<html>
<head><title>Testing Sessions page 1</title></head>
<body>
<?php
$_SESSION[‘session_var’] = “testing”;
echo “This is a test of the sessions feature.
<form action=’sessiontest2.php’ method=’POST’>
<input type=’hidden’ name=’form_var’
value=’testing’>
<input type=’submit’ value=’go to next page’>
</form>”;
?>
</body></html>
and here's the second page called sessiontest2.php:
Code: Select all
<?php
session_start();
?>
<html>
<head><title>Testing Sessions page 2</title></head>
<body>
<?php
echo “session_var = {$_SESSION[‘session_var’]}<br>\n”;
echo “form_var = {$_POST[‘form_var’]}<br>\n”;
?>
</body></html>
when i opened sessiontest1.php, the page was simply blank.
Posted: Sun Aug 21, 2005 3:01 am
by Nucleus
im sorry, but you got it wrong...
sessions dont travel between pages, what you did in the files you included is called trasferring data between two pages.
you are using a form, if ou notice, in the forn tag there is the mehod atrribute, is specifies the method of transfer. you are using the POST method. when you use the POST method the data from the form will show up in an array called $_POST (suprisingly

).
to access the data do:
(its an exapmle, you can do anything with it

)
good day.
Moshe.
Posted: Sun Aug 21, 2005 4:44 am
by feyd
Nucleus wrote:im sorry, but you got it wrong...
sessions dont travel between pages
what's your source of information on that?
that's
exactly what sessions are for.
Posted: Sun Aug 21, 2005 5:54 am
by raghavan20
try to use almost all of your php scripts on top of the the html tags even before the doctype.
new session variables can be created using $_SESSION[$session_variable_name] = $some_value;
you dont need session_register; its need only when you local or global variable in a file has to be accessed across all pages; then you register that variable as a session variable using session_register($variable_name).
once you feel, you no more need to use the variable/s as session variable/s then you unregister using session_unregister($variable_name).
this function might also be useful session_is_registered()
http://uk2.php.net/manual/en/function.s ... stered.php
Posted: Sun Aug 21, 2005 6:12 am
by Sander
As for your question, this should fix it:
Code: Select all
<?php
session_start();
if(!isset($_SESSION['var']))
{
// first time we access this page; the variable does not exist yet
$_SESSION['var'] = 0;
}
else
{
$_SESSION['var']++;
}
echo "<P>You've been here {$_SESSION['var']} times.</p>";
?>
Posted: Sun Aug 21, 2005 7:54 am
by Nucleus
also, i have to say i made a typeo:
sessions dont travel between pages
should actually be:
sessions dont travel between pages threw form...
Posted: Sun Aug 21, 2005 11:08 am
by laticca8
Sander wrote:As for your question, this should fix it:
Code: Select all
<?php
session_start();
if(!isset($_SESSION['var']))
{
// first time we access this page; the variable does not exist yet
$_SESSION['var'] = 0;
}
else
{
$_SESSION['var']++;
}
echo "<P>You've been here {$_SESSION['var']} times.</p>";
?>
i copied and pasted this code, opened the browser and saw "You've been here 0 times." that worked fine. when i refreshed the page (even after erasing the temporary files), it still said "0" times.
what's going on? just for reference, i'm using an IIS server and viewing on my localhost.
Posted: Sun Aug 21, 2005 11:16 am
by feyd
your browser, IIS, or something in-between may have removed or ignored the cookie header starting a session typically generates. Try placing a self-referencing ($_SERVER['PHP_SELF']) link in that code, it may help..
Posted: Sun Aug 21, 2005 11:21 am
by laticca8
feyd wrote:your browser, IIS, or something in-between may have removed or ignored the cookie header starting a session typically generates. Try placing a self-referencing ($_SERVER['PHP_SELF']) link in that code, it may help..
again, i'm new to this, so please forgive my ignorance. i only know where to place this self-referencing link when using a form.

where in the immediatley preceding example by Sander would i place such a link?
Posted: Sun Aug 21, 2005 11:30 am
by feyd
simply appending one link this should do it:
Code: Select all
echo '<a href="' . $_SERVER['PHP_SELF'] . '">again!</a>';
Posted: Sun Aug 21, 2005 11:44 am
by laticca8
i used this script, but the number still remained "0" after numerous revisits.
Code: Select all
<?php
session_start();
echo '<a href="' . $_SERVER['PHP_SELF'] . '">again!</a>';
if(!isset($_SESSION['var']))
{
// first time we access this page; the variable does not exist yet
$_SESSION['var'] = 0;
}
else
{
$_SESSION['var']++;
}
echo "<P>You've been here {$_SESSION['var']} times.</p>";
?>
Posted: Sun Aug 21, 2005 11:51 am
by nielsene
Is error_reporting turned on?
Posted: Sun Aug 21, 2005 11:55 am
by laticca8
nielsene wrote:Is error_reporting turned on?
yes my ini file has this line:
error_reporting = E_ALL