Variables persistent through two pages

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

Post Reply
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Variables persistent through two pages

Post by Sphenn »

Hi,

I'm trying to get a variable across multiple pages, and right now it's not working. I'm trying to build a login feature where the user enters their username and password, then a TOS page appears, and then, if they accept the terms, are logged in. Right now, the problem I have is that going from the TOS page to logging them in is on a different page, and the variables that need to be checked are lost when changing pages. Is there anyone who know how I could move the variables with the page? I've considered cookies, but I've never used them, and it seems like a lot of work for such a (seemingly) simple task.

Thanks for any input

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

Post by feyd »

use session data: session_start()
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Post by Sphenn »

Doh...

It's so simple I completely missed it. Just one question...can you think of any other way to do this? Not that sessions won't cut it for me, but I've been thinking about this for so long, I'm kind of intrigued.

Thanks again,

Sphenn
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

cookies

or if you wanted (NOT RECOMMENDED) track users by their IP
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Post by Sphenn »

Hi,

Cookies was along the same line as sessions, but even harder. And IP's were definately out. Thanks for the input though. I think I'm going to give sessions a try.

Thanks

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

Post by feyd »

feasible ways:
  • cookies (insecure)
  • sessions (more secure)
  • hidden form (insecure)
shwetha004
Forum Newbie
Posts: 12
Joined: Mon Aug 06, 2007 10:39 pm

Post by shwetha004 »

hi evryone!!

im kinda new to php,and im developing an online banking system.
i have the same question as Sphenn,and i have tried the solutions suggested in this thread.i am attempting to use the session method to retain a variable value across 2 forms.but somehow im not getting the desired output

i have 3 pages..the 1st page basically allows users to input their particular.the variable which i want to retain is in this page,input thru a field called nric.

the second page has some php which checks the fields.i want to retain the nric value from page 1,and pass it to page3..

in my second page,i have this code

Code: Select all

$ic= $_POST['nric'];

session_register();
session_start();
$_SESSION['ic'] =$_POST['nric'];
and in my 3rd page i have:

Code: Select all

session_start();
echo "ic num is:".$_SESSION['ic'];
so the 3rd page is supposed to display the nric that was input in the 1st page.but somehow the value is lost..im not sure why this is happening.if anyone could point out wer is my mistake,that would b great.maybe there is some error in my code...
thank you.. :)
shwetha004
Forum Newbie
Posts: 12
Joined: Mon Aug 06, 2007 10:39 pm

Post by shwetha004 »

hi all...

i switched to hidden fields to achieve the same effect..

so now my page1.php has:

Code: Select all

echo '
<input type="hidden" id="icNum" name="icNum" value="<?php echo $ic;?>" > ' ;
the reason im including the form in a php script is,i only want this part of the form to be displayed after certain validation is done.

and my page 2.php has:

Code: Select all

$ic = $_POST['icNum'];
echo "Ic num from hidden field:".$ic;
im trying to just output the hidden file value..to check whether it gets passed..
but apparently it doesnt,[s]coz[/s] because im getting an empty space..

i have narrowed down the problem to:

Code: Select all

value="<?php echo $ic;?>"
my guess is, since that coding is already in an echo statement starting with ', so somehow all the single n double quotes are conflicting. :( and i have tried using different combinations of single n double quotes,but none of it is working.and when i attempt to insert the $ic variable into my database,the value that is saved is: <?php echo $ic;?>.maybe only using quotes is not enough..what about slashes?.im really stumped..cant figure out how to get over this..would be very grateful if anyone can point me in the right direction.
thank you so much :)
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
shwetha004
Forum Newbie
Posts: 12
Joined: Mon Aug 06, 2007 10:39 pm

Post by shwetha004 »

hello all..

first of all,sorry about the AOL speak..i will avoid it in future..anyway,i managed to solve the problem..
instead of printing the whole form in a php echo statement,i kind of separated the code into different chunks,some html,some php..so,now my code reads:

Code: Select all

//some validation
//if the validation is successfull,the next part of the form will be displayed.
?>

Code: Select all

*i placed all the html code here..to display the form.
then to perform some database functions,i start the php code with <?php again..
maybe this is not the most efficient way,but at least its working..if anyone can show me a better wat to do it,i'll be happy to learn..
Post Reply