Reputation: 3819
I have the a php file that contains the following
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.
The question is how to move the data to the file that will do the insertion of data into database.
Upvotes: 0
Views: 105
Reputation: 560
you need to start session at the beginning to avoid this warning message.
session_start();
Upvotes: 1
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
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
put "session_start();" top of the page.
dont show or use echo before header("Location: url");
another answer: header location not working in my php code
Upvotes: 2