PHP redirect

Discussion in 'PHP' started by ziplock122949, Jun 14, 2008.

  1. #1
    I need to take the next step with my php redirect. Right now, I need to put lines of code in for every link I want to create. I found a site that has a redirect that would cut my time significantly.

    Lets say I have a few affiliates and each affiliate uses a link where you only need to change the item number. Sometimes the item number is at the end of the url and sometimes it is somewhere in the middle. I would like to give a php file 2 id's, site=affiliatesname&item=1234.

    how would i modify the following file to get this to work? I dont even know if it is possible for the array to substitute in the item# in the string.

    <?php
      $site = $_GET['site'];
      $item = $_GET['item'];
    
      $sitelink = array(
    	"Affiliate_a" => "http://www.affiliate_a.com/item/the-item-number",
    	"Affiliate_b" => "http://www.affiliate_b.com/products/the-item-number&morejunk",
    );
    	
    	header("Location:".$sitelink[$site]);
        exit;
    ?> 
    PHP:

     
    ziplock122949, Jun 14, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can easily insert variables into strings just like what you did with header("Location".$sitelink[$site])). You can either do the ".$item." or just $item directly in it since you're using double quotes (variables will parse in double quotes). If you go with the latter method it's recommended you surround it with curly braces {} so that no other text accidentally hooks onto the variable name and makes PHP think you're trying to use a variable that doesn't exist.

    <?php
      $site = $_GET['site'];
      $item = $_GET['item'];
    
      $sitelink = array(
        "Affiliate_a" => "http://www.affiliate_a.com/item/{$item}",
        "Affiliate_b" => "http://www.affiliate_b.com/products/{$item}&morejunk",
    );
       
        header("Location:".$sitelink[$site]);
        exit;
    ?>
    PHP:
     
    zerxer, Jun 14, 2008 IP
  3. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    ok, thanks for the help. I spent a few hours today watching some basic php videos (have been picking things up as i go).
     
    ziplock122949, Jun 14, 2008 IP