Jump to content

Redirect to another page


ze1d

Recommended Posts

Hi all,I'm trying to do this (for my database search engine). If query < 2 then redirect the user to the main page (Exactly like the RESPONSE.REDIRECT method in vb).I'm trying to do the following:

function checkquery($string){	if (strlen($string) < 2)	{		header("Location: DEFAULT.php");	}}

BUT this is giving me the following error:"Warning: Cannot modify header information - headers already sent by .."Is there anyway to make it work simply as the one in vb?Thanks very much,zeid

Link to comment
Share on other sites

Two things:1. You can't send headers once you already outputted information, unless you either turn on output buffering or put the header before outputting anything.2. You are using strlen() which returns the number of characters in a string and not the number of results. If you want to return the number of results, you can use mysql_num_rows().

Link to comment
Share on other sites

I tried putting my conditional statment and the header thing before all tags (before html) .. but still the same problem
Not just before any HTML. Before any "echo", "print", etc. statements. Anything that eventually prints something on the screen.Anyway, why use a function for such a simple test? Why not put it directly with the rest of the logic? You'll slightly increase performance that way.
Link to comment
Share on other sites

why .. I can't put an if statement to check for the condition like this:<?php$search = $_GET["q"];if (strlen($search) < 2){header("Location: DEFAULT.php");}?><html>etc........
Why not? You CAN of course!For the error, ensure you also don't have any whitespace before the "<?php".... ensure that the very first characters of your document are "<?php".
Link to comment
Share on other sites

thats not working

<?php$search=$_GET["q"];if(strlen($search)<2){header("Location:DEFAULT.php");}?><html><head>

its giving me the same error and not redirecting .. I'm sure there is nothing before the <?php tag!

Link to comment
Share on other sites

Strange. On my machine, if I try to send a redirect to "index.php" in the same folder, I get HTTP error 500, and this doesn't happen if I redirect to any other file.Is "DEFAULT.php" set as a directory index? That is, is it shown when you type in the name of the (sub)folder it is in? If so, then redirect to that folder instead. If it's in the same folder, redirect to ".". In code:

<?php$search=$_GET["q"];if(strlen($search)<2){header("Location: .");}?>

I don't know why typing the name of the directory index file doesn't work, yet the above does :) .

Link to comment
Share on other sites

The error message will tell you exactly where the offending output is. There's no need to guess, that's what error messages are for. Read the PHP tips thread in this forum, scroll down to the bottom to see a description of how to solve that error.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...