ze1d Posted December 9, 2007 Share Posted December 9, 2007 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 More sharing options...
zppblood Posted December 9, 2007 Share Posted December 9, 2007 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 More sharing options...
ze1d Posted December 9, 2007 Author Share Posted December 9, 2007 yeah i'm using strlen() because i want to check if the user wrote a query or an empty string. Link to comment Share on other sites More sharing options...
ze1d Posted December 9, 2007 Author Share Posted December 9, 2007 I tried putting my conditional statment and the header thing before all tags (before html) .. but still the same problem Link to comment Share on other sites More sharing options...
zppblood Posted December 9, 2007 Share Posted December 9, 2007 Even a space before/after your PHP code is considered output. Are you including files before it too that is outputting? Link to comment Share on other sites More sharing options...
boen_robot Posted December 9, 2007 Share Posted December 9, 2007 I tried putting my conditional statment and the header thing before all tags (before html) .. but still the same problemNot 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 More sharing options...
ze1d Posted December 9, 2007 Author Share Posted December 9, 2007 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........ Link to comment Share on other sites More sharing options...
boen_robot Posted December 9, 2007 Share Posted December 9, 2007 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 More sharing options...
ze1d Posted December 9, 2007 Author Share Posted December 9, 2007 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 More sharing options...
zppblood Posted December 9, 2007 Share Posted December 9, 2007 Are you using Notepad with an encoding of something like UTF-8? Link to comment Share on other sites More sharing options...
ze1d Posted December 9, 2007 Author Share Posted December 9, 2007 hmm yep?? Link to comment Share on other sites More sharing options...
boen_robot Posted December 9, 2007 Share Posted December 9, 2007 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 More sharing options...
zppblood Posted December 9, 2007 Share Posted December 9, 2007 That may be the problem then. If you want to use UTF-8, then use some other text editor like Notepad2. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 10, 2007 Share Posted December 10, 2007 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 More sharing options...
Anders Moen Posted December 10, 2007 Share Posted December 10, 2007 Put this on the top your file (over everyting, except session_start if you have that):ob_start();and at the bottom:ob_end_flush();This should fix your problem Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.