TedW Posted March 11, 2010 Share Posted March 11, 2010 Greetings all, I'm trying to get a simple script working and can't figure out why it's not. The idea is that anybody can enter their own search term and get redirected to the same page but with /search/TERM attached to the url. Well, it does redirect them back to the original page, but it fails to add the /search/TERM to the url. Here's the form that I can add to any page in any directory... <form name="goto" method="post" action="/goto.php" enctype="application/x-www-form-urlencoded"> Create your own /search/ <input type="text" name="textfield"> <input type="submit" name="Submit" value="Go!"> <br />(Separate multiple terms with a hypen (-). Do not use spaces.) </form> and here is the goto.php script that does the redirecting... /** * goto.php */ /* Get URL to "goto" from POST var */ $url = $_POST["url"]; if ($url == "") { /* If "goto" URL is not set, then set it to the refering page */ $url = $_SERVER["HTTP_REFERER"]; } elseif (!ereg("^http\:\/\/", $url) && !ereg("^https\:\/\/", $url) && !ereg("^ftp\:\/\/", $url)) { /* if protocol of http or https or ftp is not specified, set it to http */ $url = $_SERVER["HTTP_REFERER"] . "/search/" . $url; } /* "goto" the specified URL */ header("Location: $url"); ?> Link to comment https://forums.phpfreaks.com/topic/194849-need-help-with-simple-http-redirect/ Share on other sites More sharing options...
TedW Posted March 11, 2010 Author Share Posted March 11, 2010 Well, I got most of it figured out. Only problem now is that if the user has already done one search the second search is attached to the url of the previous search. For instance, if they searched for 'Hammer' they would get example.com/Hand-Tools/search/Hammer. Then from that url they search for 'Screwdriver' they would get example.com/Hand-Tools/search/Hammersearch/Screwdriver, which of course is a broken url. Here is the code I have at present... The search form <form name="goto" method="post" action="/goto.php" enctype="application/x-www-form-urlencoded"> <input type="text" name="url" size="35"> <input type="submit" name="Submit" value="Submit" > </form> and the php script if (stristr($_SERVER["HTTP_REFERER"], 'search') === TRUE) { $origurl = stristr($_SERVER["HTTP_REFERER"], 'search'); } else {$origurl = $starturl;} $url = $_POST["url"]; if ($url == "") { /* If "goto" URL is not set, then set it to the refering page */ $url = $_SERVER["HTTP_REFERER"]; } elseif (!ereg("^http\:\/\/", $url) && !ereg("^https\:\/\/", $url) && !ereg("^ftp\:\/\/", $url)) { /* if protocol of http or https or ftp is not specified, set it to http */ $url = $origurl . "search/" . $url; } /* "goto" the specified URL */ header("Location: $url"); I'm using the function stristr to remove 'search' from the url before adding it again, along with the new search term, but that doesn't remove the previous search term - they will end up with a url like example.com/Hand-Tools/search/HammerScrewdriver (which may return some interesting results, but not likely what they are looking for). The php manual mentions using before_ with the function to strip 'search' and everything after it, but they don't mention how to use it, so any samples or anything (http://www.php.net/manual/en/function.stristr.php - before_needle). So I spent the past few hours searching google for any sort of sample. It seems a gazillion sites copy/pasted that part of the manual just to get poor saps like me to find their website, but likewise they don't provide any sort of sample or instructions on how to use it. I tried every way I can imagine but I just don't know the format. So... can somebody here please tell me how to use this magical before_ feature with stristr? I would really, really appreciate it. Thanks Link to comment https://forums.phpfreaks.com/topic/194849-need-help-with-simple-http-redirect/#findComment-1024649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.