"Go" Summary of the method of passing variables in PHP page

Source: Internet
Author: User
Tags ming
Summary of methods for transferring variables between pages in PHP "Go"

Summary of methods for transferring variables between pages in PHP:
in the actual site development , we often encounter in a page defined variables , on another page also to be used , This requires that we pass the variables between the pages . here are some common ways to :

First, use the hidden in form form to hide the field. This method is to transfer the variables to be passed in the form of the way again, for example, after the user login, his information such as $username,$id and so on the information form to send to the next page, of course, In many cases, such as text,option and so on, the box body does not want to be seen, you can use the hidden input field. Here is an example, assuming that the $username is sent to this page for verification of the last page and the variables that are validated on this page, you want to pass to the next page, you can create a file test4.php :
Zhang Ming ";? >

test5.php:
Hello , $username, welcome to PHP Self-study network !";? >
Open the Apache Server, enter http://127.0.0.1/test4.php on the button on the page, You will find that the value of the $username on the new page has been sent over.
of course, the weakness of this method is obvious, it will have to use a form again, and on the test5.php Web page want to refresh the words to resend, which is obviously not very friendly.

Second, use the session. Later, I looked up some articles about the session and thought it was a good way to pass the variables. Here is an example to illustrate:
Create a new file test6.php:
Initializes a Session
Session_register ("username"); Register a session variable
$username = " Zhang Ming "; The value of the variable to pass
echo " go and see ";
?>
test7.php:
Initializes a Session
echo " Hello , $username, welcome to PHP Self-study web !";? >// display of variables passed over
when using this method, pay attention to: should be placed at the front of the file, i.e." there should be nothing in front, including spaces, which is why I failed to debug many times.
This variable can only be passed in the currently open browser, and will be passed in the newly opened window, just as follows:
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 to your presence. !";? >

Third, through cookies
Create a filetest9.php
Zhang Ming ");// put username set to Cookies in to
echo " go and see ";? >
file test5.php
Establish test5.php:
Hello , $username, welcome to PHP Self-study network !";? >
Summary : The above is only a few common methods , There are many methods , such as the use of database , Memcache and so on . Do not introduce too much here .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.