Jump to content

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

?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.