You are here: php » global variables
Global variables?
- Written By
- bos
- Submitted At
- 2009-11-04 12:41:38
- Num Views
- 620
- Category
- PHP
|
Is there a place where I can store a string that relates to where the user is in my app? Something other than saving it to a file/db or post/get from page to page? It will remain for the life of the app, be used by the whole application and will be accessed/changed everytime a user navigates. By bos @ 2009-11-04 12:41:38
|
|
Yes, it is called session variables. Session variables are saved on the server and the client just has an key to retrieve those values. This is all handled by PHP If I look at your previous posts you need to use the $_SERVER in conjunction with the $_SESSION variable. To see all the values of a variable in PHP use the following code: the pre tags are purely for displaying it properly formatted as it actually removes formatting of HTML (preformatting) it has the same effect as viewing the source. The print_r function actually prints out a variable recursively. echo "<pre>"; print_r($_SERVER); echo "</pre>"; In the server variable you will find the previous url and current url. (referer and request uri) Anyway back to your question... To use Session variables all you need to do is start the session at the top of your file with the following command: session_start(); Then you can use the variable $_SESSION as an array. ex. $_SESSION['previousUrl'] = 'index.php'; or $_SESSION['previousUrl'] = $_SERVER['REFERER']; So on page1.php you will have: <?php session_start(); //do stuff here $_SESSION['valueToRemember'] = 'abc'; ?> On page2.php you will have: <?php session_start(); echo $_SESSION['valueToRemember']; ?> Page 2's result will be: abc Let me know if this is what you needed? By PHPin24 @ 2009-11-04 14:53:04
|
|
perfect!!! Thank you. By bos @ 2009-11-04 15:57:08
|
