我有一个注册用户的注册表单,如果注册完成,应该重定向到 index.html(主页)。
问题:一旦按下提交按钮,页面刷新和表单获取重置,除非我按 CTRL + SHIFT + R 然后将我重定向到 index.html,否则不会重定向。似乎一旦按下提交按钮,会话 ID 就没有设置。
我正在学习 $_SESSION、$_GET 和 $_POST 非常简单,但我相信我已经正确完成了所有操作,那么为什么页面不保存 iD 并将我重定向到 index.php?
代码:
get_access.php
//Registration
$reg_error = '';
if(isset($_POST['password']) && isset($_POST['email']) && isset($_POST['first']) && isset($_POST['last']) && isset($_POST['username']))
{
if (strlen($_POST['password']) > 0 && strlen($_POST['email']) > 0 && strlen($_POST['first']) > 0 && strlen($_POST['last']) > 0 && strlen($_POST['username']) > 0)
{
$registration = $Wall->userRegistration($_POST['password'],$_POST['email'],$_POST['first'],$_POST['last'],$_POST['username']);
if($registration)
{
$_SESSION['uiD'] = $registration;
header("location:index.php");
} else {
$reg_error = "<span class=''>Error registering</span>";
}
}
}
<form method="post" action="" class="gainLeftForm">
<label class="smallFirst">
<strong>First and last name</strong>
<input type="text" placeholder="First" name="first" />
</label>
<label class="smallLast">
<input type="text" placeholder="Last" name="last" />
</label>
<label>
<strong>Choose your username</strong>
<input type="text" name="username" id="username" />
<span class="atemail">@oddify.co</span>
<div id="status"></div>
</label>
<label>
<strong>Create an password</strong>
<input type="password" name="password" id="password" />
<div><?php echo $reg_error; ?></div>
</label>
<label>
<strong>Enter Email</strong>
<input type="text" name="email" id="email"/>
</label>
<label>
<button type="submit">Sign up</button>
</label>
</form>
</div>
</div>
这是我用来向 MySQL 提交数据的函数,它设置正确,只是没有将我重定向到 index.php
PDO 公共功能:
public function userRegistration($password, $email, $first, $last, $username)
{
$sth = $this->db->prepare("SELECT uiD FROM users WHERE username = :username OR email = :email");
$sth->execute(array(':username' => $username,':email' => $email));
$row = $sth->fetch(PDO::FETCH_ASSOC);
if($sth->rowCount() == 0) {
$salt = substr(str_replace('+', '.', base64_encode(sha1(microtime(true), true))), 0, 22); // create a random salt
$hash = crypt($password, '$2a$12$' . $salt); // hash incoming password - this works on PHP 5.3 and up
$sth = $this->db->prepare("INSERT INTO users( password, email, first, last, username ) VALUES ( :hash_pass, :email, :first, :last, :username)");
$sth->execute(array(':hash_pass' => $hash, ':email' => $email, ':first' => $first, ':last' => $last, ':username' => $username));
$sth = $this->db->prepare("SELECT uiD FROM users WHERE username = :username");
$sth->execute(array(':username' => $username));
$me = "me";
$sth = $this->db->prepare("INSERT INTO user_friends (friend_one,friend_two,role) VALUES ( :uid, :uid1, :me )");
$sth->execute(array(':uid' => $row['uiD'], 'uid1' => $row['uiD'], 'me' => $me));
} //$sth->rowCount() == 0
else {
return $row['uiD'];
}
编辑1:
文件顶部..
session_start();
if(!empty($_SESSION['uiD'])) {
header("location:index.php");
}