这应该是一个简单的 PHP mySQL 问题(我假设它有点像 PHP 和 mySQL 101,但我找不到答案)...
我有一个我创建的注册表单,当填写完表单并将其提交到 register_user.php 页面时。该页面上的代码是这样的:
<?php
$con = mysql_connect("my_host","my_username","my_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
// username and password sent from form
//NEVER Remove the mysql_real_escape_string. Else there could be an Sql-Injection!
$firstname=mysql_real_escape_string($_POST['firstname']);
$lastname=mysql_real_escape_string($_POST['lastname']);
$email=mysql_real_escape_string($_POST['email']);
$password=mysql_real_escape_string($_POST['password']);
$sql="INSERT INTO my_table (firstName, lastName, email, password)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]', '$_POST[password]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
session_start();
// Register $myusername, $mypassword and redirect to file "profile.php"
$_SESSION['firstname']='$_POST[firstname]';
$_SESSION['lastname']='$_POST[lastname]';
$_SESSION['email']='$_POST[email]';
$_SESSION['password']='$_POST[password]';
header("location:profile.php");
?>
如您所见,它重定向到一个名为 profile.php 的页面。在 profile.php 上,我试图通过以下方式回显会话变量:
<?php echo "Hello". $_SESSION['firstname']; ?>
我假设变量在从 register_user.php 重定向到 profile.php 时失去了它的价值(至少这是我迄今为止从我的研究中收集到的)。我想我必须session_start();
在 profile.php 上再次声明变量,但我不知道如何将变量设置为适当的值。在 register_user.php 中,我能够使用上一页表单中的 post 值,但是当它重定向到 profile.php 时,我不能再这样做了。
有人可以解释我如何在 profile.php 上检索这些值吗?(我再次觉得这是一个非常基本的问题,但我仍在学习,所以要温柔哈哈 :))