[Switch] methods for transferring variables between pages in php summary methods for transferring variables between pages in php Summary: in actual website development, we often encounter variables defined on a page, another page also needs to be used, so we need to pass variables between pages. the following describes some common methods: 1. use hidden in form to hide the field. This method is used to summarize the variables to be passed in the form and then [convert] The variable transfer method between pages in php.
Summary of the variable transfer methods between pages in php:
In actual website development, we often encounter variables defined on one page and used on another page. Therefore, we need to pass variables between pages. the following describes some common methods:
1. use hidden in form to hide the field. In this way, the variables to be passed are transmitted again in the form. for example, after a user logs in, the user sends his or her information, such as $ username and $ id, to the next webpage, of course, in many cases, if the boxes such as text and option do not want to be seen again, you can use a hidden input field. The following is an example. assume that $ username is a variable sent to this page for authentication and verified on this page. to transfer it to the next page, you can create the file test4.php:
Test5.php:
Open the apache server, enter http: // 127.0.0.1/test4.php, and click the button on the page. The value of $ username on the new webpage has been transferred.
Of course, the weakness of this method is obvious. it also needs to use another form and re-send it if you want to refresh the page in test5.php. this is obviously unfriendly.
II. use session. Later, I searched for some articles about the session and thought it was a very good way to pass variables. The following is an example:
Create a new file test6.php:
Session_register ("username"); // register a session variable
$ Username = "Zhang Ming"; // variable value to be passed
Echo "go and see ";
?>
Test7.php:
Echo "Hello, $ username, welcome to php self-learning network! ";?> // Display the passed variables
Note the following when using this method: It should be placed at the beginning of the file, that is" This variable can only be passed in the currently opened browser. if you want to transfer it in the newly opened window, you just need to do the following:
File: test8.php
Session_register ("username ");
$ Username = "Zhang Ming ";
Echo "go and see"; // pass the id value of the current session to the next window.
?>
Test7.php
Echo "Hello, $ username, welcome! ";?>
3. use cookies
Create file test9.php
Echo "go and see";?>
File test5.php
Create test5.php:
Summary: The above are just some common methods, and there are many other methods, such as database and memcache, which will not be described too much here.