Connecting Tech Pros Worldwide Help | Site Map

Saving data between pages

  #1  
Old July 17th, 2005, 05:58 AM
Lamp
Guest
 
Posts: n/a
Is it possible to save session values between pages? I did a little script
to let the users login. I use this script to check username and password the
user introduces in a form in the main page, index.php.
If a user is found with username and password, than I create
$_SESSION['Login'] and assign it a value of true, else I assign it a value
of false. If a user is non registered, I send it to the proper page to
register, RegistraUtente.php, if he is registered yet, I send him back to
index.php
But here in index.php $_SESSION['Login'] has no value.

Here is the script I wrote:

<?php

session_start();

include("dbconnect.php");

$tabella="table";

$dbname="Zucca";

if (!isset($_SESSION['Login']))

{

$_SESSION['Login']=false;

}

$IdUser=session_id();

$data=date('Y-m-d');

if (!ISSET($HTTP_COOKIE_VARS["PiOnLine"]))

setcookie("PiOnLine",$IdUser,time()+60*60*24*365);

else

$IdUser=$HTTP_COOKIE_VARS["PiOnLine"];

mysql_select_db($dbname,$db);

$username=$_POST["username"];

$password=$_POST["password"];

$query="Select Username, UserPassword from $tabella where
('$password'=UserPassword) and ('$username'=Username)";

$result=mysql_query("$query") or die(mysql_error());

// Se esiste l'utente con username e password allora consenti il login

if (mysql_num_rows($result)==1)

{

$_SESSION['Login']=true;

session_write_close();

$url="index.php";

header ("Location: $url");

}

else // Invia l'utente sulla pagina di registrazione

{

$_SESSION['Login']=false;

session_write_close();

$url="RegistraUtente.php";

header ("Location: $url");

}

?>


Franz





  #2  
Old July 17th, 2005, 05:59 AM
Eric Stein
Guest
 
Posts: n/a

re: Saving data between pages


The whole point of session variables is inter-pageview preservation. You
just aren't using the session vars just right. Where you did:

$_SESSION['Login']=true;

You should have done:

$Login = true;
session_register("Login");

Later, pages can access it as $HTTP_SESSION_VARS["Login"]. I'm not sure if
there are differences between $HTTP_SESSION_VARS and $_SESSION - check the
PHP docs @ http://www.php.net and remember to provide a logout:

$Login=false;
session_register("Login");

You can also log the user out by using session_destroy().
~Eric

My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt

"Lamp" <lamp@dina.it> wrote in message
news:HQvic.138340$rM4.5430596@news4.tin.it...[color=blue]
> Is it possible to save session values between pages? I did a little script
> to let the users login. I use this script to check username and password[/color]
the[color=blue]
> user introduces in a form in the main page, index.php.
> If a user is found with username and password, than I create
> $_SESSION['Login'] and assign it a value of true, else I assign it a value
> of false. If a user is non registered, I send it to the proper page to
> register, RegistraUtente.php, if he is registered yet, I send him back to
> index.php
> But here in index.php $_SESSION['Login'] has no value.
>
> Here is the script I wrote:
>
> <?php
>
> session_start();
>
> include("dbconnect.php");
>
> $tabella="table";
>
> $dbname="Zucca";
>
> if (!isset($_SESSION['Login']))
>
> {
>
> $_SESSION['Login']=false;
>
> }
>
> $IdUser=session_id();
>
> $data=date('Y-m-d');
>
> if (!ISSET($HTTP_COOKIE_VARS["PiOnLine"]))
>
> setcookie("PiOnLine",$IdUser,time()+60*60*24*365);
>
> else
>
> $IdUser=$HTTP_COOKIE_VARS["PiOnLine"];
>
> mysql_select_db($dbname,$db);
>
> $username=$_POST["username"];
>
> $password=$_POST["password"];
>
> $query="Select Username, UserPassword from $tabella where
> ('$password'=UserPassword) and ('$username'=Username)";
>
> $result=mysql_query("$query") or die(mysql_error());
>
> // Se esiste l'utente con username e password allora consenti il login
>
> if (mysql_num_rows($result)==1)
>
> {
>
> $_SESSION['Login']=true;
>
> session_write_close();
>
> $url="index.php";
>
> header ("Location: $url");
>
> }
>
> else // Invia l'utente sulla pagina di registrazione
>
> {
>
> $_SESSION['Login']=false;
>
> session_write_close();
>
> $url="RegistraUtente.php";
>
> header ("Location: $url");
>
> }
>
> ?>
>
> Franz[/color]


  #3  
Old July 17th, 2005, 05:59 AM
Chung Leong
Guest
 
Posts: n/a

re: Saving data between pages


"Eric Stein" <no@spam.com> wrote in message
news:c6k1ji$2ano$1@news.wplus.net...[color=blue]
> The whole point of session variables is inter-pageview preservation. You
> just aren't using the session vars just right. Where you did:
>
> $_SESSION['Login']=true;
>
> You should have done:
>
> $Login = true;
> session_register("Login");[/color]

No, $_SESSION['Login'] = true should work perfectly fine. I'm not entirely
sure what the problem is. I suspect it has something to do with setting the
cookie. In general when you're using session variables, you should keep your
hand off the cookie. Stay away from session_register() too.


  #4  
Old July 17th, 2005, 06:00 AM
Eric Stein
Guest
 
Posts: n/a

re: Saving data between pages


Chung,
Sorry, I didn't know - but I've been using session_register() for a long
time and it's always worked. Why shouldn't I use it?
~Eric

My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:scmdnYz1ucEFKhDd4p2dnA@comcast.com...[color=blue]
> No, $_SESSION['Login'] = true should work perfectly fine. I'm not entirely
> sure what the problem is. I suspect it has something to do with setting[/color]
the[color=blue]
> cookie. In general when you're using session variables, you should keep[/color]
your[color=blue]
> hand off the cookie. Stay away from session_register() too.[/color]


  #5  
Old July 17th, 2005, 06:00 AM
Daniel Tryba
Guest
 
Posts: n/a

re: Saving data between pages


Eric Stein <no@spam.com> wrote:[color=blue]
> Chung,
> Sorry, I didn't know - but I've been using session_register() for a long
> time and it's always worked. Why shouldn't I use it?[/color]

Because the manual says so :)
<q src='http://nl2.php.net/session_register'>
Caution

If you want your script to work regardless of register_globals, you need
to instead use the $_SESSION array as $_SESSION entries are
automatically registered. If your script uses session_register(), it
will not work in environments where the PHP directive register_globals
is disabled.
</q>

Also look at the other warnings.

--

Daniel Tryba

  #6  
Old July 17th, 2005, 06:00 AM
.:Ninja
Guest
 
Posts: n/a

re: Saving data between pages


Eric Stein wrote:
[color=blue]
> The whole point of session variables is inter-pageview preservation. You
> just aren't using the session vars just right. Where you did:
>
> $_SESSION['Login']=true;
>
> You should have done:
>
> $Login = true;
> session_register("Login");
>
> Later, pages can access it as $HTTP_SESSION_VARS["Login"]. I'm not sure
> if there are differences between $HTTP_SESSION_VARS and $_SESSION - check
> the PHP docs @ http://www.php.net and remember to provide a logout:
>
> $Login=false;
> session_register("Login");
>
> You can also log the user out by using session_destroy().
> ~Eric
>
> My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt
>
> "Lamp" <lamp@dina.it> wrote in message
> news:HQvic.138340$rM4.5430596@news4.tin.it...[color=green]
>> Is it possible to save session values between pages? I did a little
>> script to let the users login. I use this script to check username and
>> password[/color]
> the[color=green]
>> user introduces in a form in the main page, index.php.
>> If a user is found with username and password, than I create
>> $_SESSION['Login'] and assign it a value of true, else I assign it a
>> value of false. If a user is non registered, I send it to the proper page
>> to register, RegistraUtente.php, if he is registered yet, I send him back
>> to index.php
>> But here in index.php $_SESSION['Login'] has no value.[/color][/color]

From the posted script the problem isn't immediately obvious. Other than
maybe the following. Are you testing the script on your local box running a
web server? Make sure that you access your script through
http://127.0.0.1/scriptname.php and not http://localhost/scriptname.php
because: If you session_start() on a "localhost" page, that session is not
available to a "127.0.0.1" page. On Apache, I can access
http://localhost/login.php no problem, but when I send the variable to a
relative path from login.php, Apache changes the hostname to the loopback
interface, 127.0.0.1, which of course the session won't work for.

Just a thought :)

..:Albe

--
http://www.ninja.up.ac.za
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing data between pages and querying a database in asp.net roo answers 2 October 20th, 2006 11:15 AM
Passing data between pages? Smokey Grindle answers 7 June 23rd, 2006 10:05 PM
Transfer data between pages Fernando Lopes answers 4 November 19th, 2005 09:51 AM
Saving user data between requstes RA answers 2 November 18th, 2005 05:50 AM