If statement doesn't seem to work

I have the following php code

if ("http://houston-asp.com/reviews.php" === $referrer) {
  header( "location: 	submit_a_review.php");
  exit();
} else if ("http://houston-asp.com/providers.php" === $referrer) {
  header( "location: 	become_a_provider.php");
  exit();
} else {
  header( "Location: index.php");
  exit();
}

and

echo $referrer;

which produces

http://www.houston-asp.com/become_a_member.html

but the redirection never happens.

I dont see the problem

Your code works as expected on my local system. Is it being called ON index.php?

Try adding the following to make sure that index.php is in the same path, otherwise add full URL.

echo getcwd();

Honestly, I think we need to see the whole script. Every branch of that conditional results in an exit(), but we clearly didn’t exit because echo $referrer still executes, which means the conditional as a whole never executed.

In short, the snippets OP posted are fine. The problem is elsewhere in the code he hasn’t shown us.

sorry, didn’t know you wanted to see it

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$date_created = date('Y-m-d');

$referrer = $_SERVER['HTTP_REFERER'];

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {

$sql = "INSERT INTO members (
name,
email,
password,
type_id,
created,
display
) VALUES (
:name,
:email,
:password,
1,
:created,
1
)";

$stmt = $dbh->prepare($sql);


$stmt->execute(array(
':name' => $name,
':email' => $email,
':password' => $password,
':created' => $date_created
));

	  $_SESSION['email']=$email;
	  $_SESSION['name']=$name;
	  $_SESSION['id']=$dbh->lastInsertId();

	  $headers  = "From: $m_name\r\n"; 
	  $headers .= "Content-type: text/html\r\n";
	  
	  $message = "<table width='50%'>";	  
	  $message .= '<tr><td width="50%">Name</td><td width="50%">'.$name.'</td></tr>';
	  $message .= '<tr><td>Email</td><td>'.$email.'</td></tr></table>';
		  
	  mail("admin@houston-asp.com","New Member",$message,$headers);

	  if ("http://houston-asp.com/reviews.php" === $referrer) {
		  header( "location: 	submit_a_review.php");
		  exit();
	  } else if ("http://houston-asp.com/providers.php" === $referrer) {
		  header( "location: 	become_a_provider.php");
		  exit();
	  } else {
		  header( "Location: index.php");
		  exit();
	  }
} catch(PDOException $e) {
echo '<div class="panel panel-default"style="margin:75px auto; width:960px">';	
echo '<div class="panel-body">';
echo '<h4 class="text-danger">';
echo '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>';
if ($e->getCode() == '23000') {
  echo " That email address is allready in use.</h4>";
  echo "<a href='/login.html'>Login</a><br><br>";
} else {
  echo ' The email address ({$Email}), and/or that password is not recognized, sorry.</h4>';
}
echo "<a href='/become_a_member.html'>Become a member</a><br><br>";
echo '</div></div>';
  // Log the error. echo 'Error: ' . $e->getMessage();
}
?>

So the INSERT statement works, then I set SESSION variables…
Are those ok? (the setting of the last 1?)
I get the email, but why wouldn’t the if statement run?

Thanks…

Unless your using it beforehand, you need to use session_start() at the start of the script

Try temporarily adding error_reporting(), etc

<?php 
error_reporting(-1); 
ini_set('display_errors',1);

// also as @SpacePhoenix mentioned
session_start();

$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$date_created = date('Y-m-d');


// also
} else {
  header( "Location: index.php"); 
  echo '<br />File: '  .__FILE__;
  echo '<br />Line: ' .__LINE__;
  exit();

ok, heres the whole page

<?php
session_start();
include("db/configPDO.php");

error_reporting(-1); 
ini_set('display_errors',1);

$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$date_created = date('Y-m-d');

$referrer = $_SERVER['HTTP_REFERER'];

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<title>HoustonASP: Become A Member</title>
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/style.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">HoustonASP</a>
    </div>
    <div class="navbar-collapse collapse">
     <ul class="nav navbar-nav">
       <li><a href="/">Home</a></li>
       <li><a href="/providers.php">Providers</a></li>
       <li><a href="/community">Community</a></li>
       <li><a href="/reviews.php">Reviews</a></li>
     </ul>
     <?php
	 if(!isset($_SESSION['email']))  {
	 ?>
      <form class="form-inline pull-right" action="CheckLogin.php" method="post" style="padding-top:10px">
        <div class="form-group form-group-sm">
        <label class="sr-only" for="email">Email address</label>
        <input type="email" class="form-control input-sm" name="email" placeholder="Email" id="email" size="20" maxlength="50">
        </div>
        <div class="form-group form-group-sm">
        <label class="sr-only" for="password">Password</label>
        <input type="password" class="form-control input-sm" name="password" placeholder="Password" id="password" size="10" maxlength="15">
        </div>
        <button type="submit" class="btn btn-default btn-sm">Sign in</button>
      </form>
       <?php } else { ?>
       <div class="pull-right">
        <span class="badge">You are logged in as <?=$_SESSION['name']?></span>
        <a href="logout.php" class="btn btn-danger btn-sm" role="button" style="margin:9px">Logout</a>
       </div>
      <?php } ?>        
      </div><!--/.navbar-collapse -->
  </div>
</div>
<div class="jumbotron">
</div>
<?php 

try {

$sql = "INSERT INTO members (
name,
email,
password,
type_id,
created,
display
) VALUES (
:name,
:email,
:password,
1,
:created,
1
)";

$stmt = $dbh->prepare($sql);


$stmt->execute(array(
':name' => $name,
':email' => $email,
':password' => $password,
':created' => $date_created
 ));

 echo getcwd();
 echo '<br>';
 echo $referrer;

	  $_SESSION['email']=$email;
	  $_SESSION['name']=$name;
	  $_SESSION['id']=$dbh->lastInsertId();

	  $headers  = "From: $m_name\r\n"; 
	  $headers .= "Content-type: text/html\r\n";
	  
	  $message = "<table>";	  
	  $message .= '<tr><th>Name</th><td>'.$name.'</td></tr>';
	  $message .= '<tr><th>Email</th><td>'.$email.'</td></tr></table>';
		  
	  mail("admin@houston-asp.com","New Member",$message,$headers);

	  if ("http://houston-asp.com/reviews.php" === $referrer) {
		  header( "location: 	submit_a_review.php");
		  exit();
	  } else if ("http://houston-asp.com/providers.php" === $referrer) {
		  header( "location: 	become_a_provider.php");
		  exit();
	  } else {
		   header( "Location: index.php"); 
		  echo '<br />File: '  .__FILE__;
		  echo '<br />Line: ' .__LINE__;
		  exit();
	  }
} catch(PDOException $e) {
echo '<div class="panel panel-default"style="margin:75px auto; width:960px">';	
echo '<div class="panel-body">';
echo '<h4 class="text-danger">';
echo '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>';
if ($e->getCode() == '23000') {
  echo " That email address is already in use.</h4>";
  echo "<a href='/login.html'>Login</a><br><br>";
} else {
  echo ' The email address ({$Email}), and/or that password is not recognized, sorry.</h4>';
}
echo "<a href='/become_a_member.html'>Become a member</a><br><br>";
echo '</div></div>';
  // Log the error. echo 'Error: ' . $e->getMessage();
}
?>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

the result


once I fill in the form

any ideas?
I corrected the error on line 121, now i get

turn off error reporting now that you have fixed the error

In PHP, header() must be called before any actual output is sent, such as normal HTML tags. That’s why you’re getting this warning when calling header() after having already sent your form HTML.

The solution is that all your form submission logic – the db insert, the mail sending, the header redirect – should be moved above your form HTML (probably just above the doctype). And you should wrap your form submission logic in an if ($_SERVER['REQUEST_METHOD'] === 'POST') condition. Otherwise, your form submission code tries to run even when there isn’t a form submission.

thanks…

I would be tempted to leave error reporting but to remove display errors because all subsequent errors will be logged.

you mean leave this

error_reporting(-1);

but where does it get logged to?
Cause id like to look at it at times to make sure its always working

Try searching for php error_log or error_log_string.

I am using a mobile at the moment and not easy :frowning:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.