Create new user form nor redirecting

Hi,

I have just created a script to allow an admin person create a password and username for a second person for member area. But it is not redirecting properly. For instance, once the user puts in his username and password it should send the message “you have successfully created a new user”. Instead it re-directs to the admin area and the welcome message welcomes the admin person.

Anyone see where im going wrong here?

<?php  require_once("includes/session.php"); 
include("includes/functions.php");
require_once("includes/connect.php");
confirm_logged_in();
if (isset($_POST['submit'])) { //if the page has been submitted submit is the submit button
$errors = array();
$required_fields = array('username', 'password');
foreach($required_fields as $fieldname){
if (!isset($_POST[$fieldname])){
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('username' =>30);
foreach($fields_with_lengths as $fieldname => $maxlength){
if(($_POST[$fieldname]) > $maxlength){
$errors[] = $fieldname;}
}
 $username = ($_POST['username']);
 $password = ($_POST['password']);
 
 //Now we need to ensure the password is encrypted before it is sent to the database. We use an inbuilt incrypter called sha1:
 $hashed_password = sha1($password);

if (empty($errors)){
//query
$query = "INSERT INTO staffmember (
username, password
)VALUES(
 '{$username}','{$hashed_password}'
)";
// execute query
$result = mysqli_query($connection, $query);
// test to see if the update worked 
if ($result) {
// success
$message = "The user was successfully created";
redirect_to("login.php");
}else{
//update failed
$message = "The user could not be created";
$message .= "<br/>".mysqli_error($connection);
}
}else{
// errors occured
if(count($errors)==1){
$message = "There was 1 error in the form";
}else{
$message = "There were " .count($errors). " errors in the form";
}
} // end of issset post
}else{
$username = "";
$password = "";
} 
?>

The re-direct function is as follows:

function redirect_to($location = NULL){
 if($location != NULL){
 header("Location:{$location}");
 exit;
 }
 } 

session script:
<?php session_start();

function logged_in(){
return isset($_SESSION[‘user_id’]);
redirect_to(“staff.php”);
}

function confirm_logged_in(){
if(!logged_in()){
redirect_to(“login.php”);
}
}
?>

thanks for your help:cool:

Try getting rid of the curly braces in the header(‘Location’) call:

function redirect_to($location = NULL){
	if($location != NULL){
		header("Location: $location");
		exit();
	}
} 

Hi,

tried this but it makes no difference at all. also i noticed i hadnt but the brackets after the exit (i.e. exit() ) but this has made no difference either.

Any other ideas people:(

The code


if ($result) {
 // success
 $message = "The user was successfully created";
 redirect_to("login.php");
}

only redirects to login.php. You do not output your $message anywhere. You need to redirect to some page e.g. thanks.php where “you have successfully created a new user” would be output most probably …