This is a discussion on scope over multiple pages within the PHP Language forums, part of the PHP Programming Forums category; I need to preserve some values over several pages. I want the user to enter his username, password and the ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I need to preserve some values over several pages. I want the user to
enter his username, password and the IP address of the MySQL server at the beginning of a series of pages. I want all the pages to know these values. I want all the pages to display these values so the user can change them or not. The "series" of pages is an HTML page that gathers this information. This is sent to a PHP server page. The server page may have to re-load itself if the user chooses to re-sort the data. If I had it the way I want it, the username, password and IP would be gathered in the HTML page at the beginning and be available to the server page even when it re-loads itself. I also want the HTML page (if visited again) to display the original values in the text boxes. I have tried $_POST, hidden variables within forms, $GLOBAL (which I couldn't get to work at all) and probably some other ways to do this. Needless to say, I couldn't get the results I wanted from any of these. HELP! |
|
|||
|
Jeff Sandler wrote:
> I need to preserve some values over several pages. I want the user to > enter his username, password and the IP address of the MySQL server at > the beginning of a series of pages. I want all the pages to know these > values. I want all the pages to display these values so the user can > change them or not. > The "series" of pages is an HTML page that gathers this information. > This is sent to a PHP server page. The server page may have to re-load > itself if the user chooses to re-sort the data. If I had it the way I > want it, the username, password and IP would be gathered in the HTML > page at the beginning and be available to the server page even when it > re-loads itself. I also want the HTML page (if visited again) to > display the original values in the text boxes. > I have tried $_POST, hidden variables within forms, $GLOBAL (which I > couldn't get to work at all) and probably some other ways to do this. > Needless to say, I couldn't get the results I wanted from any of these. > HELP! :-)) Use session variables. <?php // page1.php session_start(); $name = isset($_SESSION['name']) ? $_SESSION['name'] : ''; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['name'] = $_POST['name']; exit('Go to <a href="page2.php">page 2</a>.'); } echo <<<HTML <form action="page1.php" method="post"> <input type="text" name="name" value="$name"/> <input type="submit"/> </form> HTML; ?> <?php // page2.php session_start(); if (!isset($_SESSION['name'])) { echo 'You need to enter your name. '; exit('Please goto <a href="page1.php">page 1</a>.'); } echo 'Welcome ', $_SESSION['name']; ?> -- USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes : |
|
|||
|
ditto what Pedro said,
here's my version (cut/paste from a previous post). http://www.php.net/reserved.variables $_SESSION is particularly cool, but you need to run the session_start() function first before it works. Unfortunately the above page neglects to mention that and it has caught some people out. As a newbie, you will want to make sure you get to know PHP's session handling capabilities -- IT WILL SAVE YOU A LOT OF WORK! http://www.php.net/manual/en/ref.session.php remember that before the $_SESSION superglobal came along, it was neccesary to use the other session functions all the time, now you can do things like. <?php session_start(); if(!isset($_SESSION["uname"])) { if(blnAutheticated($_POST)) { // use form data $_SESSION["uname"] = $_POST["uname']; } else { header("Location: login.php"); // redirect them die(); } } echo "You are currently logged in as ".$_SESSION["uname"]; ?> obviously the above code requires you to write a blnAutheticated() function which accepts an associative array that it will search for authentication tokens. function blnAutheticated($arrPost) { $blnAuth = false; $sqlCheck = "SELECT * FROM users WHERE " ."uname = '".$arrPost["uname"]."'"; ."uname = '".$arrPost["uname"]."'"; if($arrPost) } |