Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy newbie help if/else statement, maybe?

    I have tried many variations on this and even tried a switch instead of the if/elseif/else, somehow, only the first header(location:url) is read no matter what I try. I have used if{} if{} if{}, if{}elseif{}else{}, if{if{}else{}}else{}, and anything else I could think of! I'm new to PHP, but have been programming for years.

    What's up??

    Code:
    if (is_null($_SERVER["HTTP_REFERER"]) or !array_key_exists("HTTP_REFERER",$_SERVER)) {
    	include ('includes/inc_header.php');
    	var_dump($_SERVER["HTTP_REFERER"]);
    } else {
    	if ($_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") {
    		header("Location: http://www.moontimefarms.com/ncm/index.php");
    	} else {
    		if ($_SERVER["HTTP_REFERER"]="http://www.blueoats.com") {
    			header("Location: http://www.moontimefarms.com/bo/test.php");
    		} else {
    			include ('includes/inc_header.php');
    			var_dump($_SERVER["HTTP_REFERER"]);
    		}
    	}
    }
    Thanks for the help!
    Nicki

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You should echo out the referrer so that you know exactly what it is that you are trying to match aginst. do this BEFORE the If statement

    Also I would agree that you would be better off using a switch for this than if elses
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I changed to this and now the variable doesn't seem to be read at all. How do I "echo out the referrer"?

    Code:
    if (array_key_exists("HTTP_REFERER",$_SERVER)) {
    	switch($_SERVER["HTTP_REFERER"])
    	{
    	case "http://www.naturalchoicemarket.com":
    		header("Location: http://www.moontimefarms.com/ncm/index.php");
    		break;
    	case "http://www.blueoats.com":
    		header("Location: http://www.moontimefarms.com/bo/test.php");
    		break;
    	case "":
    		include ('includes/inc_header.php');
    		var_dump($_SERVER["HTTP_REFERER"]);
    		break;
    	}
    } else {
    	include ('includes/inc_header.php');
    	var_dump($_SERVER["HTTP_REFERER"]);
    }

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by akmagnolia View Post
    How do I "echo out the referrer"?
    PHP Code:
    echo $_SERVER["HTTP_REFERER"]; 
    Are you a Help Vampire?

  • #5
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks,
    as it is now, it still isn't working. here's the code
    PHP Code:
    if (array_key_exists("HTTP_REFERER",$_SERVER)) {
        echo 
    $_SERVER["HTTP_REFERER"];
        if (
    $_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") {
            
    header("Location: http://www.moontimefarms.com/ncm/index.php");
        } elseif (
    $_SERVER["HTTP_REFERER"]="http://www.blueoats.com") {
                
    header("Location: http://www.moontimefarms.com/bo/test.php");
        } else {
                include (
    'includes/inc_header.php');
                
    var_dump($_SERVER["HTTP_REFERER"]);
        }
    } else {
        include (
    'includes/inc_header.php');
        
    var_dump($_SERVER["HTTP_REFERER"]);

    so stuck

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    PHP Code:
        if ($_SERVER["HTTP_REFERER"]="http://www.naturalchoicemarket.com") { 
    Oops. A single "=" is an assignment. A test for equality is two.

    PHP comparison operators
    Are you a Help Vampire?

  • #7
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    OK, so I changed the = to ==. It appears to be reading the variable since it does output to the screen, but there is still something wrong with the redirect part.

    PHP Code:
    if (array_key_exists("HTTP_REFERER",$_SERVER)) {
        echo 
    $_SERVER["HTTP_REFERER"];
        
    $my_place=$_SERVER["HTTP_REFERER"];
        echo 
    $my_place;
        if (
    $my_place=="http://www.naturalchoicemarket.com") {
            
    header("Location: http://www.moontimefarms.com/ncm/index.php");
        } elseif (
    $my_place=="http://www.blueoats.com") {
                
    header("Location: http://www.moontimefarms.com/bo/test.php");
        } else {
                include (
    'includes/inc_header.php');
                
    var_dump($_SERVER["HTTP_REFERER"]);
        }
    } else {
        include (
    'includes/inc_header.php');
        
    var_dump($_SERVER["HTTP_REFERER"]);

    The situation is that I have 3 domains I want to share a hosting account. The 2 domains I want to redirect are "forwarded with masking" to the main domain (perhaps this is the problem?) so the HTTP_REFERER variable should always exist for them.

    so completely frustrated - seems like this should be much simpler!

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I'm really not sure what to tell you there. The only webhost I use handles the redirection and masking stuff through the web panel, but I don't use any of it. I don't have any idea what happens to the referer variables or anything in those cases.
    Are you a Help Vampire?

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    $my_place $_SERVER["HTTP_REFERER"];
    if(
    $my_place == "http://www.naturalchoicemarket.com"){
        
    header("Location: http://www.moontimefarms.com/ncm/index.php");
    }elseif(
    $my_place == "http://www.blueoats.com"){
        
    header("Location: http://www.moontimefarms.com/bo/test.php");
    }else{
        include (
    'includes/inc_header.php');
        
    var_dump($_SERVER["HTTP_REFERER"]);

    don't echo anything before. I would prefere to doit with mod_rewrite instead of this.

    best regards

  • #10
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks.
    the solution I found uses a string function and somehow this works...not sure why

    PHP Code:
    if (array_key_exists("HTTP_REFERER",$_SERVER)) {
        
    $my_place=$_SERVER["HTTP_REFERER"];
        
    $url=substr_count($my_place,"naturalchoicemarket");
        
    $url2=substr_count($my_place,"blueoats");
        if (
    $url>0) {
            
    header("Location: http://www.moontimefarms.com/ncm/index.php");
        } elseif (
    $url2>0) {
            
    header("Location: http://www.moontimefarms.com/bo/index.html");
        } else {
                include (
    'includes/inc_header.php');
                
    var_dump($_SERVER["HTTP_REFERER"]);
        }
    } else {
        include (
    'includes/inc_header.php');
        
    var_dump($_SERVER["HTTP_REFERER"]);


  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by akmagnolia View Post
    thanks.
    the solution I found uses a string function and somehow this works...not sure why

    PHP Code:
    if (array_key_exists("HTTP_REFERER",$_SERVER)) {
        
    $my_place=$_SERVER["HTTP_REFERER"];
        
    $url=substr_count($my_place,"naturalchoicemarket");
        
    $url2=substr_count($my_place,"blueoats");
        if (
    $url>0) {
            
    header("Location: http://www.moontimefarms.com/ncm/index.php");
        } elseif (
    $url2>0) {
            
    header("Location: http://www.moontimefarms.com/bo/index.html");
        } else {
                include (
    'includes/inc_header.php');
                
    var_dump($_SERVER["HTTP_REFERER"]);
        }
    } else {
        include (
    'includes/inc_header.php');
        
    var_dump($_SERVER["HTTP_REFERER"]);

    array_key_exists("HTTP_REFERER",$_SERVER) is always true you don't need to check it and second else is useless..
    about comperison using substr_count, will return how many times the second string is find inside the first and will work without problem.

    http://www.php.net/manual/en/function.substr-count.php

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •