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

时间:2013-07-20 04:37:51

标签: php html mysql pdo

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

问题:一旦按下提交按钮,页面刷新并形成get重置,除非我按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");
} 

1 个答案:

答案 0 :(得分:1)

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'];
    }
}