dominod Posted June 29, 2010 Share Posted June 29, 2010 Hi I want to redirect with PHP using a string.. this is how my code looks like: <?php header( 'Location: $redir' ) ; ?> <html> <body> <head> <title><?=$search?></title> </head> <?php function wordsExist(&$string, $words) { foreach($words as &$word) { if(stripos($string, $word) !== false) { return true; } } return false; } if (wordsExist($search, array('word1','word2','word3'))) { $redir = $search; } else { $redir = "http://www.google.com/search?q=$search"; } ?> The problem is that it redirects to mydomain.com/$redir and not the URL from the $redir string. Please help me to understand this Sincerly, Daniel Haukås Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/ Share on other sites More sharing options...
plutomed Posted June 29, 2010 Share Posted June 29, 2010 Have you put http:// in the string url? Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078912 Share on other sites More sharing options...
dominod Posted June 29, 2010 Author Share Posted June 29, 2010 Well.. yes .. The thing is that it doesnt read the string as it shuld. Here is what it shuld be reading: $redir = "http://www.google.com/search?q=$search"; Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078913 Share on other sites More sharing options...
MadTechie Posted June 29, 2010 Share Posted June 29, 2010 it should be header( "Location: $redir" ) ; NOT header( 'Location: $redir' ) ; note the single quotes won't parse the variable also the whole script should look like this <?php function wordsExist(&$string, $words) { foreach($words as &$word) { if(stripos($string, $word) !== false) { return true; } } return false; } if (wordsExist($search, array('word1','word2','word3'))) { $redir = $search; //this should be a URL! }else{ $redir = "http://www.google.com/search?q=$search"; } //Header MUST be sent before ANY output! header("Location: $redir") ; exit; //Anything below this pointless ?> Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078919 Share on other sites More sharing options...
plutomed Posted June 29, 2010 Share Posted June 29, 2010 Well.. yes .. The thing is that it doesnt read the string as it shuld. Here is what it shuld be reading: $redir = "http://www.google.com/search?q=$search"; I mentioned that because without the http:// it tries to redirect to the domain on your site. :/ Also doesn't the header( "Location: $redir" ) ; need to be header( "Location: ".$redir ) ; ? Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078920 Share on other sites More sharing options...
dominod Posted June 29, 2010 Author Share Posted June 29, 2010 Thanks guys but it still wont redirect .. Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078922 Share on other sites More sharing options...
MadTechie Posted June 29, 2010 Share Posted June 29, 2010 You may want to review your "wordsExist" function! However if the google one also fails then you have either failed to supply some details are missed something from the examples given.. either way more details are needed! Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078923 Share on other sites More sharing options...
dominod Posted June 29, 2010 Author Share Posted June 29, 2010 Wait! It works now thanks to the code by MadTechie! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/#findComment-1078924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.