sessions problem

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

laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

sessions problem

Post 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?
Nucleus
Forum Newbie
Posts: 3
Joined: Sun Aug 21, 2005 2:33 am

Post by Nucleus »

well...
when your using a session you dont have to officially register it its enough to do:

Code: Select all

$_SESSION["var"] = 1;
as above, when your using a session variable, you just do:

Code: Select all

$_SESSION["var"]++;
and offcourse start the session with session_start()...
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post 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.
Nucleus
Forum Newbie
Posts: 3
Joined: Sun Aug 21, 2005 2:33 am

Post 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:

Code: Select all

echo $_POST["form_field_name"];
(its an exapmle, you can do anything with it :D )

good day.
Moshe.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post 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>";

?>
Nucleus
Forum Newbie
Posts: 3
Joined: Sun Aug 21, 2005 2:33 am

Post 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...
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post 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. :oops: where in the immediatley preceding example by Sander would i place such a link?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

simply appending one link this should do it:

Code: Select all

echo '<a href="' . $_SERVER['PHP_SELF'] . '">again!</a>';
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post 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>"; 

?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Is error_reporting turned on?
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

nielsene wrote:Is error_reporting turned on?
yes my ini file has this line:

error_reporting = E_ALL
Post Reply