Jump to content

Retaining variables accross pages


yrrahxob

Recommended Posts

I am building my own web site and on the main page, there is an option to login to a users' account if they so desire.  This is not a requirement.  After they login, they can browse through products and and then to the shopping cart.  When they are ready to checkout, they are presented with a form asking for name and address and other information.  What I want to do is when the form is presented and they are logged in, the form will be populated with the information retrieved when they logged in.

below is the validation/login script I am working on.  I want to keep the values of the array $user_info during the entire session and I have register_globals disabled.  Someone please help.

<?php

function validate_user($user_email, $user_password){

$row=array();

$q = db_query("SELECT ss_first_name, ss_last_name, ss_email, ss_phone, ss_address_1, ss_address_2, ss_city, ss_state, ss_zip_1, ss_zip_2 FROM ".USER_TABLE." WHERE ss_email='$user_email' AND ss_password='$user_password'") or die (db_error());
$row = db_fetch_row($q);
return $row;

}

if(isset($_POST["login_submitted"])){
$user_info=validate_user($_POST["user_email"], $_POST["user_password"]);
}

?>
Link to comment
https://forums.phpfreaks.com/topic/21064-retaining-variables-accross-pages/
Share on other sites

In order to ratin variables across pages you could use sessions.

Put [code]session_start()[/code] before anything is output (eg on the top of the page) and define the variables like $_SESSION['something']. You would then be able to call it on another page (which has ran session_start() too) as $_SESSION['something'].
lookup sessions


quick example.

test.php

[code]
<?php session_start();

$name="redarrow";

$name=$_SESSION['name']=$name;

echo"<a href='test_result.php'>please see my name</a>";

?>
[/code]

test_result.php
[code]
<?php session_start();

echo"Hello there my name is $name";

?>
[/code]
I don't think that would work

This will work:

test.php
[code]
<?php
session_start();
$_SESSION['name'] = "something";
echo"<a href='test_result.php'>please see my name</a>";
?>
[/code]

test_result.php
[code]
<?php
session_start();
echo "Hello there my name is {$_SESSION['name']}";
?>
[/code]
[quote author=redarrow link=topic=108394.msg436046#msg436046 date=1158505296]
[code]
<?php session_start();

$name="redarrow";

$name=$_SESSION['name']=$name;

echo"<a href='test_result.php'>please see my name</a>";

?>
[/code]
[/quote]

Why on earth would do [code=php:0]$name=$_SESSION['name']=$name[/code] for? I see no logic in that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.