...

View Full Version : Resolved A redirect problem with PHP5?



sea4me
03-03-2009, 06:28 AM
Ok I have this script that works perfectly on my other host (PHP4) but after I switched to another host (PHP5) my page seems to stop redirecting.

Here is my code:

<?php
mysql_connect("localhost", "******", "*******") or die(mysql_error());
mysql_select_db("*****") or die(mysql_error());
$customerid="1"; //it's part of a system but now it's just static
$cart="1"; //it's part of a system but now it's just static
if (!empty($_GET['action'])){ $action = $_GET['action']; }

if ($action=="notes"){
if (!empty($_POST['cart'])){ $cart = $_POST['cart']; } else { $cart="1"; }
if (!empty($_POST['jobname'])){ $jobname = $_POST['jobname']; }
if (!empty($_POST['add'])){ $add = $_POST['add']; }
if (!empty($_POST['city'])){ $city = $_POST['city']; }
if (!empty($_POST['state'])){ $state = $_POST['state']; }
if (!empty($_POST['zip'])){ $zip = $_POST['zip']; }
if (!empty($_POST['tel'])){ $tel = $_POST['tel']; }
if (!empty($_POST['email'])){ $email = $_POST['email']; }
if (!empty($_POST['status'])){ $status = $_POST['status']; }
if (!empty($_POST['memo'])){ $memo = $_POST['memo']; }

$query = "SELECT * FROM notes WHERE CUSTOMERID = '".$customerid."' AND CART = '".$cart."' AND `ORDERID`='0'";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);

if ($count == 0) {
$query = "INSERT INTO `notes` ( `CUSTOMERID` , `CART` , `JOBNAME` , `TEL` , `ADDRESS` , `CITY`, `STATE`, `ZIP`, `EMAIL`, `STATUS`, `MEMO`) VALUES ('".$customerid."', '".$cart."', '".$jobname."', '".$tel."', '".$add."', '".$city."', '".$state."', '".$zip."', '".$email."', '".$status."', '".$memo."')";
$sql = mysql_query($query) or die(mysql_error());
}
else {
$query = "UPDATE `notes` SET `JOBNAME` = '".$jobname."',`TEL` = '".$tel."', `ADDRESS` = '".$add."', `CITY` = '".$city."', `STATE` = '".$state."', `ZIP` = '".$zip."', `EMAIL` = '".$email."', `STATUS` = '".$status."', `MEMO` = '".$memo."' WHERE `CUSTOMERID` = '".$customerid."' AND `CART` = '".$cart."' AND `ORDERID`='0'";
$sql = mysql_query($query) or die(mysql_error());
}

header( "Location: index.html" ) ;

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title></title>
</head>

<body>
<form action="notes.php?action=notes" method="post">
<input name="cart" type="hidden" value="<?php echo $cart; ?>" />
<?php
$query = "SELECT * FROM notes WHERE CUSTOMERID = '".$customerid."' AND CART = '".$cart."' AND ORDERID = '0' LIMIT 1";
$sql = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_row($sql)) {
$jobname = $row[3];
$tel = $row[4];
$add = $row[5];
$city = $row[6];
$state = $row[7];
$zip = $row[8];
$email = $row[9];
$status = $row[10];
$memo = $row[11];
}
?>
<table style="text-align:left">
<tr>
<td colspan="2">Jobname:<br /><input name="jobname" type="text" size="40" value="<?php echo $jobname;?>" /></td>
<td colspan="1">Phone:<br /> <input name="tel" type="text" size="25" value="<?php echo $tel;?>" /></td>
</tr><tr>
<td colspan="3">Address:<br /> <input name="add" type="text" size="70" value="<?php echo $add;?>" /></td>
</tr><tr>
<td>City: <br /><input name="city" type="text" size="30" value="<?php echo $city;?>" /></td>
<td>State: <br /><input name="state" type="text" size="10" value="<?php echo $state;?>" /></td>
<td>Zip: <br /><input name="zip" type="text" size="20" value="<?php echo $zip;?>" /></td>
</tr><tr>
<td colspan="2">Email: <br /><input name="email" type="text" size="40" value="<?php echo $email;?>" /></td>
<td colspan="1">Status: <br />
<select name="status">
<option value="Estimated" <?php if(!empty($status) && $status=="Estimated"){echo "selected";} ?>>Estimated</option>
<option value="Pending" <?php if(!empty($status) && $status=="Pending"){echo "selected";} ?>>Pending</option>
<option value="Paid" <?php if(!empty($status) && $status=="Paid"){echo "selected";} ?>>Paid</option>
</select>
</td>
</tr><tr>
<td colspan="3">Memo: <br /><textarea name="memo" cols="20" rows="10"><?php echo $memo;?></textarea></td>
</tr><tr>
<td></td>
<td ><input name="cart<?php echo $cart; ?>" type="submit" value="Update Job Info" /></td>
<td></td>
</tr>
</table>
</form>
</body>

</html>

The page shows up as blank after I press submit and it stays at the same page! :mad:
Is header not allowed to be inside an if statement in PHP5?
If a create a very simple page it redirects:

<?php
header('Location: index.php' );
?>

Thank you!

oesxyl
03-03-2009, 06:43 AM
add this on the top of your page and see what's happend:


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


and check the { and } pair to match
best regards

ninnypants
03-03-2009, 06:47 AM
Just a random idea since you don't seem to be using any deprecated functions try removing the white space around your header function.
Before:

header( "Location: index.html" ) ;

After:

header("Location: index.html");

sea4me
03-03-2009, 07:06 AM
It works now after I removed the whitespaces!!!

Thank you!

sea4me
03-03-2009, 07:07 AM
I guess random ideas are very helpful :thumbsup:

sea4me
03-03-2009, 07:08 AM
WOW fixed in less than an hour!

that's a record!

snowysweb
03-03-2009, 07:30 AM
Also if you cant find the white space or simply cant be bothered to look i ALWAYS stick

exit;
straight underneath the header function.

abduraooft
03-03-2009, 08:31 AM
Off topic:

if ($action=="notes"){
if (!empty($_POST['cart'])){ $cart = $_POST['cart']; } else { $cart="1"; }
if (!empty($_POST['jobname'])){ $jobname = $_POST['jobname']; }
if (!empty($_POST['add'])){ $add = $_POST['add']; }
if (!empty($_POST['city'])){ $city = $_POST['city']; }
if (!empty($_POST['state'])){ $state = $_POST['state']; }
if (!empty($_POST['zip'])){ $zip = $_POST['zip']; }
if (!empty($_POST['tel'])){ $tel = $_POST['tel']; }
if (!empty($_POST['email'])){ $email = $_POST['email']; }
if (!empty($_POST['status'])){ $status = $_POST['status']; }
if (!empty($_POST['memo'])){ $memo = $_POST['memo']; } is equivalent to


foreach($_POST as $key=>$value){
if(!empty($value))
$$key=$value;
}

_Aerospace_Eng_
03-03-2009, 03:48 PM
Back on topic. I'm surprised you haven't been a victim of mysql injection yet. You do nothing in your code to prevent mysql injection. I suggest you read this: http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

sea4me
03-04-2009, 12:14 AM
I do have codes to prevent mySQL injection, it's just not posted to save room :thumbsup:

sea4me
03-04-2009, 04:11 AM
Ok the problem is back on my other page....

It shows up as blank....


<?php if (LoggedIn() == false) { header("Location: index.php?page=my"); exit(); } ?>

anything wrong with this code? It worked fine in my last host (PHP4)

abduraooft
03-04-2009, 05:29 AM
Did you follow oesxyl's post above?

sea4me
03-04-2009, 06:12 AM
Nevermind may last problem, it was solved by adding this to the top:


<?php
ob_start();
?>

output_buffering was turned off on the server, putting this code at the start of the page fixed it

sea4me
03-04-2009, 06:13 AM
Off topic: is equivalent to


foreach($_POST as $key=>$value){
if(!empty($value))
$$key=$value;
}

Just at a curiosity, would this affect the response and processing time?

abduraooft
03-04-2009, 07:13 AM
Just at a curiosity, would this affect the response and processing time?

Of course, system would take some extra resource/time to process a loop/function. After all, machine code is always faster than any other formats :)

sea4me
03-04-2009, 06:15 PM
So it's still better to have:

if ($action=="notes"){
if (!empty($_POST['cart'])){ $cart = $_POST['cart']; } else { $cart="1"; }
if (!empty($_POST['jobname'])){ $jobname = $_POST['jobname']; }
if (!empty($_POST['add'])){ $add = $_POST['add']; }
if (!empty($_POST['city'])){ $city = $_POST['city']; }
if (!empty($_POST['state'])){ $state = $_POST['state']; }
if (!empty($_POST['zip'])){ $zip = $_POST['zip']; }
if (!empty($_POST['tel'])){ $tel = $_POST['tel']; }
if (!empty($_POST['email'])){ $email = $_POST['email']; }
if (!empty($_POST['status'])){ $status = $_POST['status']; }
if (!empty($_POST['memo'])){ $memo = $_POST['memo']; }
rather than:

foreach($_POST as $key=>$value){
if(!empty($value))
$$key=$value;
}

I don't really care about how much work it takes, I care about how fast the code is gonna process

_Aerospace_Eng_
03-04-2009, 08:28 PM
The second would work okay but now its going to take in any post variable no matter where it comes from. Someone could get your code add their own post value and post it to your server. You should still do some type of validation on your values. Never trust the user.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum