gpt4 book ai didi

php - $_SESSION ['uiD' ] 注册后无法重定向到主页,uiD 未设置

转载 作者:行者123 更新时间:2023-11-29 13:43:34 27 4
gpt4 key购买 nike

我有一个正在注册用户的注册表单,如果注册完成,应该重定向到index.html(主页)。

问题:按下提交按钮后,页面刷新并且表单获取重置,不会重定向,除非我按 CTRL + SHIFT + R 然后将我重定向到index.html。一旦按下提交按钮, session 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)函数:

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");
}

最佳答案

userRegistration 存在两个问题。

首先,当它通过代码注册新用户时,它不会返回任何内容。其次,在该代码中,在准备查询以获取新用户的 uiD 后,它从未调用 fetch。但是,您不需要为此进行查询,可以使用 PDO::lastInsertId() 来实现。

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));

$new_id = $this->db->lastInsertId();

$me = "me";
$sth = $this->db->prepare("INSERT INTO user_friends (friend_one,friend_two,role) VALUES ( :uid, :uid1, :me )");
$sth->execute(array(':uid' => $new_id, 'uid1' => $new_id, 'me' => $me));

return $new_id;
} //$sth->rowCount() == 0
else {
return $row['uiD'];
}
}

关于php - $_SESSION ['uiD' ] 注册后无法重定向到主页,uiD 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com