Occur
Occur

Reputation:

php header redirect based on post variables

I have visitors coming to my site from 5 seperate sources, each one sends a variable in the url based on where its from, in the event these sources sends visitors I want to send them to a seperate page thats more relevant to the user

<?php
     $var = $_GET["var"];
     if( $var='site1'){
              header('Location: ' . "http://www.example.com/site1page");
     }else{
              header('Location: ' . 'http://www.example.com/othersites/&?var='.$var,);
     }
?>

however no matter what $var comes in has its going to the first header location(site1page) Can anyone explain why this is happening?

Upvotes: 0

Views: 2141

Answers (3)

Citizen
Citizen

Reputation: 12967

Additionally, I think that comma in the second header line is going to give you a parse error.

Upvotes: 0

pivotal
pivotal

Reputation: 746

If that code is the actual code you are running - it is because you are using "=", the assignment operation instead of "==" the comparison operator. PHP lets you bite yourself this way without any sort of warning.

Upvotes: 4

Tony Miller
Tony Miller

Reputation: 9159

Your code is doing assignment (the single equal sign). You want an equality test (double equals sign):

<?php if( $var=='site1') { header('Location: ' . "http://www.mysite.com/site1page"); } else { header('Location: ' . 'http://www.mysite.com/othersites/&?var='.$var,); } ?>

Upvotes: 0

Related Questions