簡體   English   中英

無法使用會話變量存儲數據

[英]Cannot Use Session Variable To Store Data

我有一個包含以下內容的php文件

  1. 一個html文件,用於收集新用戶的數據
  2. 驗證輸入數據的php函數
  3. 在數據提交之前提醒並提示用戶的javascript代碼
  4. 驗證過程之后,我想將輸入數據移動到insert.php以將數據插入數據庫。

下面是代碼

<?PHP
session_start();

if (!(isset($_SESSION['login_user']) && $_SESSION['login_user'] != '')) {

header ("Location: loginForm.php");

}

$hash;
$salt;

?>


<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="layout.css">
<script language="javascript" type="text/javascript" src="/templates/myjavascript.js"></script>

<title>Title of the document</title>
<script>
  window.onload=function(){
    document.getElementById("new_user_form").onsubmit=function(){

        if(this.pword.value === this.pword2.value)
           { alert("Are you sure you want to create a new user")
               return true;}
         else{ alert("Paswords must be the same ");
             return false;}
      }
  }

</script>
</head>
<div>
<body>

<section id="content">


<form align="center" class="form" action="create_user.php" method="post" id="new_user_form" name="new_user_form">

<ul>

<li>
<h2>Please Fill The Form</h2>

</li>



<li>
     <label for="username">User Name</label> 
        <input name="username" id="keyword" type="text" placeholder="type first name (required)" required />             

</li>

<li>
     <label for="pword">Password</label>
     <input name="pword" id="pword" type="password" placeholder="########" required />
</li>

<li>
     <label for="pword2">Confirm Password</label>
     <input name="pword2" id="pword2" type="password" placeholder="########" required />
</li>



<p>
          <input type = "reset" class="reset" name="submit"/>
          <input type ="submit" name="submit" value="submit" class="submit" "/>
        </p>    

</section>

</ul>
</form>

</body>
</div>

<?php

 /* php function to check pasword policy 
     The password has to be alphanumeric and special characters minimum 8 and max 16               
                    */                   

function checkpwdPolicy(){

      if($_POST['pword'] === $_POST['pword2']) {

  if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,16}$/', $_POST['pword'])) {
  echo '<script> alert(" password requirement not met"); </script>';

} else{

 /* we use the session varible to store the hashed password and username   */
                                                      /
 $_SESSION['pwd'] = password_hash($_POST['pword'], PASSWORD_DEFAULT);
 $_SESSION['username'] = $_POST['username'];

 header ("Location: insert_user_table.php");
  }
} 
  else {echo "passwords do not match  "; }

}

 //tis is used to call the fucntion that checks the password policy                                      

if(isset($_POST['submit']) && isset($_POST['username']) && isset($_POST['pword']) && isset($_POST['pword2']) )
{checkpwdPolicy();}


?>

</html>

但是,當我運行鱈魚時,卻顯示以下錯誤。 下面是錯誤。

在此處輸入圖片說明

問題是如何將數據移動到將數據插入數據庫的文件中。

如果您在會話啟動命令之前以及標頭(“ location:url”)之前放置任何東西或顯示任何內容。 不起作用。

只是

  1. 把“ session_start();” 頁面頂部。

  2. 不要在header(“ Location:url”)之前顯示或使用echo。

另一個答案: 標頭位置在我的php代碼中不起作用

改變你的

<?PHP

<?php

刪除空格(如果有)

session_start();
//remove this line
if (!(isset($_SESSION['login_user']) && $_SESSION['login_user'] != '')) {
//remove this line
header ("Location: loginForm.php");
//remove this line
}

刪除上述代碼之間的空格。

或者嘗試用它代替標題

<script type="text/javascript">
window.location.assign('loginForm.php');
</script>

您需要從頭開始會話,以避免出現此警告消息。

session_start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM