2017-08-01 55 views
-1

我有一個調查網頁,如果用戶完成了,我會發送他們給出的數據(作爲$_SESSION從其他頁面),我會將他/她的狀態更改爲0(他們在他/她的登錄信息之後,不能再進行調查)。用PDO發送數據後重定向到空白頁

所有這些都與PDO。但爲什麼頁面總是重定向到白色的空白頁面?

這裏是我的代碼

<?php 
session_start(); 
if (!isset($_SESSION['user'])) 
{ 
header("location:index.php"); 
} 
?> 

<?php 
require_once "condb.php"; 
?> 

<?php 
if (isset($_POST['BTN_P2'])) 
{ 
    $_SESSION['problem'] = $_POST['problem']; 
    if ($_SESSION['problem'] == "yes"){header("location:survey_3.php");} 
else 
{ 
    $sql="INSERT INTO data(time,suggest,phone,eat,problem) VALUES(?,?,?,?,?)"; 
    $stm=$cn->prepare($sql); 
    $stm->bindParam("1",$_SESSION['time']); 
    $stm->bindParam("2",$_SESSION['suggest']); 
    $stm->bindParam("3",$_SESSION['phone']); 
    $stm->bindParam("4",$_SESSION['eat']); 
    $stm->bindParam("5",$_SESSION['problem']); 

    try 
    { 
    $stm->execute(); 
     try 
     { 
     $sqlstatus="INSERT INTO login(status) VALUES(0)"; 
     $stmt=$cn->prepare($sqlstatus); 
     $stmt->execute(); 
     echo "Finish!"; 
     header('location:finish.php'); 
     } 
     catch (Exception $error) 
     { 
     echo $error->getTraceAsString(); 
     } 
    } 
    catch (Exception $e) 
    { 
    echo $e->getTraceAsString(); 
    } 
} 
} 
?> 

我缺少什麼?

編輯#1:驗證$_SESSION['user']來自何處。

<?php 
if (isset($_POST['BTN_ENTER'])) 
{ 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    $hashed_password=password_hash($password,PASSWORD_DEFAULT); 
    try 
    { 
     $stmt = $cn->prepare("SELECT * FROM login WHERE username=:username LIMIT 1"); 
     $stmt->execute(array(':username'=>$username)); 
     $result=$stmt->fetch(PDO::FETCH_ASSOC); 
     if($stmt->rowCount() > 0) 
     { 
      if(password_verify($password, $result['password'])) 
      { 
      if ($result['status']==1) 
       { 
       $_SESSION['user']=$result['name']; 
       header('location:survey.php'); 
       } 
      } 
     } 
    } 
    catch(PDOException $e) 
    { 
     echo $e->getMessage(); 
    } 
} 
+0

很可能因爲'$ _SESSION ['user']'從未設置。你可以顯示設置'$ _SESSION ['user']'的代碼嗎? – Fredster

+1

你怎麼知道它是重定向到一個空白頁面,而不是簡單地有一個錯誤,你沒有看到,因爲你沒有使用'error_reporting(E_ALL)查看錯誤;'?是否打開查看錯誤? – Rasclatt

+0

@Fredster我已經編輯來驗證$ _SESSION ['user']'來自何處。 – PaoPaoMC

回答

0

你應該嘗試打破你的腳本到函數/方法,使其更易於使用,閱讀,再利用等。當你犯了一個功能,您可以手動自行嘗試一下你實現它。一旦你確認功能的工作原理本身,那麼你扔到邏輯如果事情失敗了,你知道這並不是說功能:

/functions/suggestEatingProblem.php

<?php 
function suggestEatingProblem($con,$array) 
    { 
     $sql = "INSERT INTO `data` (`time`,`suggest`,`phone`,`eat`,`problem`) VALUES(?,?,?,?,?)"; 
     $query = $con->prepare($sql); 
     # Since you are not really doing anything special with your parameters 
     # just do the array into the execute function, it's more straight forward 
     $query->execute($array); 
    } 

/功能/addLoginStatus.php

<?php 
function addLoginStatus($con,$val) 
    { 
     # Don't need to prepare, just query 
     $con->query("INSERT INTO login (`status`) VALUES({$val})"); 
    } 

/functions/verifyUser.php

<?php 
function verifyUser($con,$username,$password) 
    { 
     $stmt = $con->prepare("SELECT `password`,`name` FROM login WHERE username = ? LIMIT 1"); 
     $stmt->execute(array($username)); 
     $result = $stmt->fetch(PDO::FETCH_ASSOC); 
     if(empty($result['password'])) 
      return false; 

     $isValid = password_verify($password, $result['password']); 
     return ($isValid)? $result['name'] : false; 
    } 

/config.php

<?php 
# Create some helpful constants 
define('DS',DIRECTORY_SEPARATOR); 
define('ROOT_DIR',__DIR__); 
define('FUNCTIONS',ROOT_DIR.DS.'functions'); 
# Start session 
session_start(); 
# Start database 
require_once(ROOT_DIR.DS."condb.php"); 

/whatever.php

<?php 
# Add config at top 
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php'); 
# Add our functions to be used 
require_once(FUNCTIONS.DS.'suggestEatingProblem.php'); 
require_once(FUNCTIONS.DS.'addLoginStatus.php'); 
# If no session, redirect 
if (!isset($_SESSION['user'])) { 
    header("Location: index.php"); 
    # Make sure to stop your script if you are done with the page 
    exit; 
} 
# Check if submission 
if(isset($_POST['BTN_P2'])) { 
    # Assign problem (trim it) 
    $_SESSION['problem'] = trim($_POST['problem']); 
    # If yes 
    if($_SESSION['problem'] == "yes") { 
     # Redirect and stop the script execution 
     header("Location: survey_3.php"); 
     exit; 
    } 
    else { 
     # Just do one "try" here... 
     try { 
      # Run your function here 
      suggestEatingProblem($cn,array(
       $_SESSION['time'], 
       $_SESSION['suggest'], 
       $_SESSION['phone'], 
       $_SESSION['eat'], 
       $_SESSION['problem'] 
      )); 
      # Run your second function here 
      addLoginStatus($cn,'0'); 
      # Redirect. You can not put content before you redirect, so 
      # remove the "Finished" echo 
      header('Location: finish.php'); 
      exit; 
     } 
     catch (PDOException $e){ 
      echo 'Error occurred: '.$e->getTraceAsString(); 
     } 
    } 
} 
else { 
    echo 'No request sent.'; 
} 

驗證部:

# Add function (see the other example to add the config) 
require_once(FUNCTIONS.DS.'verifyUser.php'); 
# If there is a submission 
if(isset($_POST['BTN_ENTER'])) { 
    try { 
     # Fetch the name (or false) 
     $user = verifyUser($cn,trim($_POST['username']),trim($_POST['password'])); 
     # If not false 
     if($user) { 
      # Assign the value 
      $_SESSION['user'] = $user; 
      # Redirect and stop execution of script 
      header('Location: survey.php'); 
      exit; 
     } 
    } 
    catch(PDOException $d) { 
     echo $e->getMessage(); 
    } 
} 
相關問題