【问题标题】:Cannot Use Session Variable To Store Data不能使用会话变量来存储数据
【发布时间】:2015-09-08 10:35:26
【问题描述】:

我有一个包含以下内容的 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>

当我运行 cod 时,会显示以下错误。 以下是错误。

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

【问题讨论】:

  • 将您的checkpwdPolicy() 放在文件上的&lt;html&gt; 之前,这样可以解决您的问题。
  • How to fix "Headers already sent" error in PHP 的可能重复项 // 请在询问之前研究错误消息——这已经讨论了无数次了。
  • @Disha V 谢谢你的建议解决了我的问题

标签: javascript php


【解决方案1】:

如果您在会话开始命令之前以及 header("location: url") 之前放置任何东西或显示任何东西。它不会工作。

简单

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

  2. 在 header("Location: url") 前不显示或使用 echo;

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

【讨论】:

    【解决方案2】:

    改变你的

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

    【讨论】:

      【解决方案3】:

      您需要从头开始会话以避免出现此警告消息。

      session_start();
      

      【讨论】:

        猜你喜欢
        • 2019-09-11
        • 2014-09-20
        • 1970-01-01
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 2014-02-07
        • 2014-10-14
        相关资源
        最近更新 更多