Little trouble with a redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Little trouble with a redirect

Post by mendenha »

Code: Select all

<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
$total_rows = mysql_num_rows($rs);

If (mysql_num_rows($rs)==0)
	header('Location:http://www.google.com');
		Else
		echo "This is a valid entry";
		?>
I'm having an issue with the above code. If the query returns 0 results I need it to redirect to another page.
Right now I'm receiving the following error.
[text]Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/prod/ccmenus/comments.php:5) in /Applications/XAMPP/xamppfiles/htdocs/prod/ccmenus/comments.php on line 53 [/text]

Not sure why it's having an issue so any help would be greatly appreciated.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Little trouble with a redirect

Post by cpetercarter »

Well, the problem is exactly what the error message is telling you. You cannot send a header if any output at all has already been sent to the browser - not even a blank space. Something on line 5 of your comments.php file has already sent something to the browser. You need to look and see what it is.
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Little trouble with a redirect

Post by mendenha »

I checked line 5 and it's calling an include file. If I comment that line out I still get the same message.

Here is the entire code for the page.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php include("../include/SEO.php"); ?>
<title>Capital City Menus &ndash; Springfield, IL - Restaurant Menus, Restaurant Maps, Restaurant Locations</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> 
<link href="../styles/default.css" rel="stylesheet" type="text/css" />
<link href="../styles/table_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("../include/opendbconnection.php"); ?>
<div id="header">
</div>
<div id="menu">
  <ul>
    <li<?PHP If(($category=="All")or($category==""))echo"class='active'";?>><a href="../ccmenus/menuview.php?category=All">All</a></li>
    <li<?PHP If($category=="American")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=American">American</a></li>
    <li<?PHP If($category == "Asian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Asian">Asian</a></li>    
    <li<?PHP If($category == "Italian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Italian">Italian</a></li>
    <li<?PHP If($category == "Mexican")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Mexican">Mexican</a></li>
    <li<?PHP If($category == "Pizza")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Pizza">Pizza</a></li>
    <li<?PHP If($category=="Steak-Seafood")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Steak-Seafood">Steak & Seafood</a></li>
    <li<?PHP If ($category == "About") echo "class='active'";?>><a href="../ccmenus/menuview.php?category=About">About</a></li>
  </ul>
</div>
<div id="content">
  <div id="colOne">
  </div>
  <div id="colTwo">
<!--This is just a simple test to see if I can pull the ID-->
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<!--This is a test to see If I can pull the name of the ID above-->
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
while ($row=mysql_fetch_array($rs))
{
echo "<td class='contact'><b>" . $row['display_name'] . "</b>";
}?>
<br />
<!--This is a test to see if the row ID is valid. If not, it will redirect to homepage.-->
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
$total_rows = mysql_num_rows($rs);

If (mysql_num_rows($rs)==0)
	echo "This is a invalid entry.";
		Else
		echo "This is a valid entry";
		?>
  </div>
 <div id="colThree">
     <?php include ("../include/colthree.php"); ?>  
</div>
<div id="footer">
<center><?php include("../include/footer.php"); ?></center>
</div>
<br />
<center><a href="http://www.dreamhost.com/green.cgi">
<img border="0" alt="Green Web Hosting! This site hosted by DreamHost."
src="https://secure.newdream.net/green1.gif" height="32" width="100" /></a></center>
</body>
</html>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Little trouble with a redirect

Post by cpetercarter »

This is what you are sending to the browser:
[syntax]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />[/syntax]
You can't send headers after you have started to send the html for the web page.
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Little trouble with a redirect

Post by mendenha »

Got ya. What should I do now?

Basically what I'm trying to do is avoid a situation where a user could change the ID path in the url.

If a user clicks on comment for a certain item it send them to www.domainname.com/comment.php?id=12
This is the ID of the current DB row for the item.

I want to stop a user from entering some random number in the URL. I know most users wouldn't try this but there is always someone out there who will.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Little trouble with a redirect

Post by cpetercarter »

I see a lot of scripts on this forum which attempt to use header('Location: ....) as a routine programming tool, and I don't like it. Sending a location header is slow and clumsy; and often there are more elegant and economic solutions available. OK, rant over.

In your case, if a user types in some random id, and the database query returns no results, why not simply display a message that no records have been found and why don't they try again? Sending the user off to google means sending him/her away from your site, and generally this is not what web design is about!
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Little trouble with a redirect

Post by mendenha »

Google was just for testing. In reality it would send them back to the homepage of my site.

The reason why I want the redirect is because the commenting software I will be using looks at the URL and determines what set of comments to load.

If a user screws around in the URL field and enters a invalid ID number the commenting software will treat it as a new URL and create a new comment section for it.
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Little trouble with a redirect

Post by mendenha »

I got the redirect working. I moved my php code to the very top of the page. Here is the code now.

Code: Select all

<?php
include("../include/opendbconnection.php");
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
$total_rows = mysql_num_rows($rs);
If (mysql_num_rows($rs)==0)
	header("Location: menuview.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php include("../include/SEO.php"); ?>
<title>Capital City Menus &ndash; Springfield, IL - Restaurant Menus, Restaurant Maps, Restaurant Locations</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> 
<link href="../styles/default.css" rel="stylesheet" type="text/css" />
<link href="../styles/table_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("../include/opendbconnection.php"); ?>
<div id="header">
</div>
<div id="menu">
  <ul>
    <li<?PHP If(($category=="All")or($category==""))echo"class='active'";?>><a href="../ccmenus/menuview.php?category=All">All</a></li>
    <li<?PHP If($category=="American")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=American">American</a></li>
    <li<?PHP If($category == "Asian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Asian">Asian</a></li>    
    <li<?PHP If($category == "Italian")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Italian">Italian</a></li>
    <li<?PHP If($category == "Mexican")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Mexican">Mexican</a></li>
    <li<?PHP If($category == "Pizza")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Pizza">Pizza</a></li>
    <li<?PHP If($category=="Steak-Seafood")echo"class='active'";?>><a href="../ccmenus/menuview.php?category=Steak-Seafood">Steak & Seafood</a></li>
    <li<?PHP If ($category == "About") echo "class='active'";?>><a href="../ccmenus/menuview.php?category=About">About</a></li>
  </ul>
</div>
<div id="content">
  <div id="colOne">
  </div>
  <div id="colTwo">
<!--This is just a simple test to see if I can pull the ID-->
<p>The query string is: <?php echo $_SERVER['QUERY_STRING']; ?></p>
<!--This is a test to see If I can pull the name of the ID above-->
<?php
$id = $_GET['id'];
$q="Select * from tbl_restaurantinfo WHERE id = '".$id."'";
$rs=mysql_query($q) or die(mysql_error());
while ($row=mysql_fetch_array($rs))
{
echo "<td class='contact'><b>" . $row['display_name'] . "</b>";
}?>
<br />
  </div>
 <div id="colThree">
     <?php include ("../include/colthree.php"); ?>  
</div>
<div id="footer">
<center><?php include("../include/footer.php"); ?></center>
</div>
<br />
<center><a href="http://www.dreamhost.com/green.cgi">
<img border="0" alt="Green Web Hosting! This site hosted by DreamHost."
src="https://secure.newdream.net/green1.gif" height="32" width="100" /></a></center>
</body>
</html>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Little trouble with a redirect

Post by cpetercarter »

Yup - that's what you need to do. Another of my rants is about doing the logic first and the webpage second. I hate it when I see html and php mixed up together in the same script.
mendenha
Forum Newbie
Posts: 19
Joined: Tue Jan 12, 2010 2:24 pm

Re: Little trouble with a redirect

Post by mendenha »

How would you suggest doing it?

I'm new to all this stuff so the more information I can get, the better.
Post Reply