2013-07-20 30 views
0

我有註冊用戶的註冊表單,如果註冊完成重定向到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,它的所有正確設置的功能,只是沒有進行重定向我的索引.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"); 
} 
+0

您是否在腳本的開頭調用'session_start()'? – Barmar

+0

是的,很抱歉,我更新了頭文件session_start()的信息。 – iBrazilian

+0

'userRegistration'註冊新用戶時不返回任何內容,僅當它在DB中找到現有用戶時纔會返回任何內容。 – Barmar

回答

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

據我所知,因爲該查詢沒有返回任何返回的最後插入的iD在函數的末尾將確保它被設置。感謝Barmar的幫助,我非常感謝你的時間。謝謝你解釋。 – iBrazilian