Forum Moderators: coopster

Message Too Old, No Replies

Passing variables in php

php

         

garywhite glw

8:49 pm on Jan 5, 2010 (gmt 0)

10+ Year Member



Yet again I return.

I have a php script with a variable that accepts a user's email address, does some stuff to it that then allows the variable to be used in a SQL query. I then need to use that email address on a later page to run a not dissimilar query. Rather than get the user to re-input the address, how can I simply hang onto the readily prepared variable for a new quey?

The address is in a cookie but when I pass the contents of the cookie into a new variable it is curiously formatted - someting like '\'email@example.com\ and is thus unusable.

I miss VB.

Cheers
Gary

optik

9:47 pm on Jan 5, 2010 (gmt 0)

10+ Year Member



Use sessions, so something like

session_start();//this creates a new session
$_SESSION['email']=$email;

then on your later page

session_start();//this opens the current session
$email=$_SESSION['email'];

garywhite glw

10:22 pm on Jan 5, 2010 (gmt 0)

10+ Year Member



Fantastic!

Works a treat. When does session information get killed?

Cheers a lot
Gary

rocknbil

3:48 am on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The default, I think, it about 25 minutes (depends on PHP config,) or for security's sake, execute session_destroy() when you're done with it. This is a good idea in a scenario where you dynamically generate the form, for example,

$frm .= '<input type="text" name="some-name" value="' . $_SESSION['some-name'] . '">';

This is useful for error states, that is, user submits form without a required field, you can redirect (ugh) back to the form and have their data prepopulated. But in a successful submit, you don't want that to happen should they revisit the form, their first thought will be paranoia: "They are saving my personal info" - which is not really the case.

So when you're done with a session, clean up after yourself with session_destroy().

garywhite glw

11:54 am on Jan 6, 2010 (gmt 0)

10+ Year Member



Thank you once again. This forum is fab.

StoutFiles

3:32 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be aware that SESSION variables have a user limit and can also bog down the server with high amounts of traffic. If you have big waves of traffic at certain time periods I would suggest monitoring performance.

garywhite glw

8:15 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



What is the user limit?