Home  >  Article  >  Backend Development  >  Transfer variables between two web pages in php

Transfer variables between two web pages in php

巴扎黑
巴扎黑Original
2016-12-01 10:41:451240browse


Variable transfer between two web pages in php

[Author: Douya added time: 2001-5-28 22:53:08]


In website design, we often use users With this method of logging in, after logging in, general websites will give different pages according to different users. When linking to a new web page of the site or opening a new web page, we will also see the user's information. It can be remembered. It is especially common now that after logging in, you will see greetings such as "XXX, good evening" when linking or opening other web pages of the site. These are of course the results of variable transmission after logging in. In the process of making my personal homepage, I have tried several methods of variable transmission, and I will write them down to discuss with you.
1. This is the stupidest method, and it was also the method I came up with when I didn’t have any contact with sessions or cookies. This method is to transfer the variables to be transferred again in the form of a form. For example, after the user logs in, his information such as $username, $id, etc. is sent to the next web page using a form. Of course, in many cases, it is If you don’t want boxes such as text and options to be seen anymore, you can use hidden input fields. The following is an example. Assume that $username is a variable sent to this page for identity verification on the previous page and verified on this page. If you want to pass it to the next page, you can create the file test4.php:





Create test5.php:

Open the apache server, enter http://127.0.0.1/test4.php and click on the button on the page. You will find $username on the new web page. The value has been transferred.
Of course, the weakness of this method is obvious. It requires another form, and if you want to refresh the test5.php web page, you have to resend it. This is obviously not very friendly.

2. Use session. Later, I searched some articles about session and felt that this was a very good way to pass variables. The following is an example to illustrate:
Create a new file test6.php:
session_register("username"); //Register a session variable
$username="Zhang Xiaoming" ; //Variable value to be passed
echo "Go and see";
?>
Create test7.php:
echo "Hello, $username, welcome!";?> //Display the passed variables
Be careful when using this method: should Put it at the front of the file, that is, there should be nothing in front of "This variable can only be passed in the currently opened browser. If you want to pass it in a newly opened window, just do it as follows:
File: test8.php
session_register("username");
$username="Zhang Xiaoming";
echo "Go and have a look" ; // Pass the current session id value to the next window.
?>
File test7.php
echo "Hello, $username, welcome!";?>

Three, through cookies. Let’s illustrate with an example:
Create the file test9.php
echo " Go take a look";?>
File test5.php
Create test5.php:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn