0

I have the a php file that contains the following

  1. an html file which is used to collect data for a new user
  2. php functions that validate the input data
  3. javascript codes that alerts and prompt the user before data submission
  4. after the validation process I want to move the input data to an insert.php to insert data into the database.

Below is the code

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

How ever when I run the cod I get the following error displayed. Below is the error.

enter image description here

The question is how to move the data to the file that will do the insertion of data into database.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
faisal abdulai
  • 3,739
  • 8
  • 44
  • 66
  • place your `checkpwdPolicy()` before `` on the file, that would solve your issue. – Disha V. Sep 08 '15 at 10:50
  • 4
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) // please _research_ error messages before asking – this has been discussed a gazillion times already. – CBroe Sep 08 '15 at 10:52
  • 1
    @Disha V thanks your suggestion solved my problem – faisal abdulai Sep 08 '15 at 11:12

3 Answers3

2

if you put any thing or show anything before session start command as well as before header("location: url"). it will not work.

simply

  1. put "session_start();" top of the page.

  2. dont show or use echo before header("Location: url");

another answer: header location not working in my php code

Community
  • 1
  • 1
Deepak
  • 178
  • 1
  • 2
  • 14
1

change your

<?PHP

to

<?php

remove spaces before it if any

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

remove spaces in between the above code.

or try this in place of your header

<script type="text/javascript">
window.location.assign('loginForm.php');
</script>
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
1

you need to start session at the beginning to avoid this warning message.

session_start();
anu g prem
  • 545
  • 5
  • 15