0

ユーザーを登録する登録フォームがあり、登録が完了したら 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");
} 
4

1 に答える 1