无法使用会话变量来存储数据

时间:2015-09-08 10:35:26

标签: javascript php

我有一个包含以下

的php文件
  1. 用于为新用户收集数据的html文件
  2. 用于验证输入数据的php函数
  3. 在数据提交之前提醒用户并提示用户的JavaScript代码
  4. 在验证过程之后我想将输入数据移动到insert.php以将数据插入数据库。
  5. 以下是代码

    <?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>
    

    当我运行鳕鱼时,我显示以下错误。 以下是错误。

    enter image description here

    问题是如何将数据移动到将数据插入数据库的文件中。

3 个答案:

答案 0 :(得分:2)

如果你在session start命令之前和header之前放置任何东西或显示任何内容(“location:url”)。它不会起作用。

简单

  1. 把“session_start();”页面顶部。

  2. 不要在标题前显示或使用echo(“Location:url”);

  3. 另一个答案:header location not working in my php code

答案 1 :(得分:1)

改变你的

<?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>

答案 2 :(得分:1)

您需要在开头启动会话以避免此警告消息。

session_start();
相关问题