faisal abdulai
faisal abdulai

Reputation: 3819

Cannot Use Session Variable To Store Data

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.

Upvotes: 0

Views: 105

Answers (3)

anu g prem
anu g prem

Reputation: 560

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

session_start();

Upvotes: 1

Syed mohamed aladeen
Syed mohamed aladeen

Reputation: 6755

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>

Upvotes: 1

Deepak
Deepak

Reputation: 178

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

Upvotes: 2

Related Questions