If else help

I am fairly new to PHP. I am unable to get the following code to assign the values to my session variables and redirect to the page depending on the value of $credit

<?php
$credit = $row_rsUser['listCredit']; //value = 1
echo ($credit); //test
if ($credit == 0) {
$_SESSION['sessListType'] = "1";
header("Location: cart.php");
}
else {
$_SESSION['sessListType'] = "2";
$_SESSION['sessListAuth'] = "1";
header("Location: listing_add.php");
}
?>

=== to test, I added the following code: ===

sessListType: <?php echo $row_rsUser['listCredit']; ?>
sessListAuth: <?php echo $_SESSION['sessListAuth']; ?>

=== I am getting the following output: ===

1 (this is from //test above)
sessListType: 1
sessListAuth: 1

==========
Any ideas as to what I’m doing incorrectly?

The header function should give an error because your echo statement already created output.
Try running your code without that statement

Also a shortcutish way of doing echo is simple

echo $variable;

You don’t need echo ($variable);

The second way just looks weird. You normally see people doing the first way (dunno if second way is an error or not.)

Ok, I removed the echo statement. But it still doesn’t seem to be functioning. Here’s what I have:

<?php
$credit = $row_rsUser['listCredit'];
if ($credit == 0) {
	$_SESSION['sessListType'] = 1;
    header("Location: cart.php");
}
else {
	$_SESSION['sessListType'] = 2;
	$_SESSION['sessListAuth'] = 1;
	header("Location: listing_add.php");
}
?>

Both session variables are being set to 1 and the header is not being redirected.

Do you have error reporting and display errors enabled?
If so are you getting a headers already sent error message?

Add an exit(); after each header() as well otherwise the script will continue its execution

<?php
$credit = $row_rsUser['listCredit'];
if ($credit == 0) {
	$_SESSION['sessListType'] = 1;
    header("Location: cart.php");
	exit();
}
else {
	$_SESSION['sessListType'] = 2;
	$_SESSION['sessListAuth'] = 1;
	header("Location: listing_add.php");
	exit();
}
?>

Here is the error message:

Warning: Cannot modify header information - headers already sent by (output started at /home/cvoffers/public_html/mycv/rd_01.php:8) in /home/cvoffers/public_html/mycv/rd_01.php on line 62

You posted under 15 lines of code. You have more to this script obviously. Post the full script. You have an echo or sometthing before your headers.

Here is the entire code:

<?php require_once('../Connections/connData.php'); ?>
<?php error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php session_start();
$_SESSION['sessListAuth'] = 0;
?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_rsUser = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsUser = $_SESSION['MM_Username'];
}
mysql_select_db($database_connData, $connData);
$query_rsUser = sprintf("SELECT ID, email, listCredit FROM mycv WHERE email = %s", GetSQLValueString($colname_rsUser, "text"));
$rsUser = mysql_query($query_rsUser, $connData) or die(mysql_error());
$row_rsUser = mysql_fetch_assoc($rsUser);
$totalRows_rsUser = mysql_num_rows($rsUser);

?>

<?php
$credit = $row_rsUser['listCredit'];
if ($credit == 0) {
	$_SESSION['sessListType'] = 1;
    header("Location: cart.php");
	exit();
}
else {
	$_SESSION['sessListType'] = 2;
	$_SESSION['sessListAuth'] = 1;
	header("Location: listing_add.php");
	exit();
}
?>

<?php
mysql_free_result($rsUser);
?>

Or whitespace in there somewhere prior to header()

It was the whitespace!

Yup that’s true. Good correction.

Thanks guys! I truly appreciate all the help.

You should be migrating your code away from using the old mysql_* extension as that extension is deprecated as of version 5.5 of PHP. Whether you migrate over to the mysqli_* extension or PDO you should be using prepared statements when dealing with any sort of user submitted data

Those statements were created by the latest version of Dreamweaver. Do you know if there is a option within Dreamweaver for mysqli?

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