Jump to content

PHP Redirection with a string


dominod

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/206220-php-redirection-with-a-string/
Share on other sites

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
?>

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 ) ;

?

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.