在PHP中通过标题传递参数 [英] Passing arguments via header in php
问题描述
我有3个档案:
login.html
login_check.php
welcome.php
在 login.html
中输入用户名和密码并提交按钮点击 login_check.php
根据 $ _ POST ['username']
和一些SQL查询。现在我已将以下代码放在 login_check.php
login_check的底部。 php
:
header('Location:welcome.php')
但是我想从<$ $ _ POST ['username']
c $ c> login_check.php 到 welcome.php
,这样我可以使用 $ _ POST ['username' ]
在我的欢迎页面。有什么办法可以传递像上面这种情况下的一个参数吗?
使用 session
,因为您会在欢迎页面上每次显示用户名称,无论您登录在欢迎页面的哪个页面。
您可以在login_check页面上设置会话,如:
session_start(); //这应该在login_check文件的顶部
//这个在重定向行之前进行
$ _SESSION ['username'] = $ _POST ['username'];
现在在欢迎页面上,您可以显示用户名称:
session_start(); //这应该在欢迎页面上。
回声`欢迎'。 $ _SESSION [用户名];
I have 3 files:
login.html
login_check.php
welcome.php
In login.html
when the username and password is entered and submit button is clicked login_check.php
checks whether the username entry is in the database on the basis of $_POST['username']
and some SQL query. Now I have put the following code at the bottom of login_check.php
login_check.php
:
header('Location:welcome.php')
But I want to pass $_POST['username']
from login_check.php
to welcome.php
so that I can make use of $_POST['username']
in my welcome page. Is there any way by which I can pass an argument like in the above case?
Use session
instead because you would be showing the user's name everytime on the welcome page no matter which page you land at welcome page.
You can set the session on login_check page like:
session_start(); // this should be on top of login_check file
// this goes just before redirect line
$_SESSION['username'] = $_POST['username'];
Now on the welcome page, you can show username like:
session_start(); // this should be on top of welcome page.
echo `Welcome ` . $_SESSION['username'];
这篇关于在PHP中通过标题传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!