insert var into header location

Discussion in 'PHP' started by zenite, Feb 21, 2008.

  1. #1
    hi, I am selling products from clickbank and am using php redirects to hide my affiliate links. I am trying to insert a variable into my php redirects for the affiliate ID so that I can change it easily.

    for example, ID.php have the following code

    
    <?php
    $aff_id = 'myID';
    ?>
    
    PHP:
    and ebook.php have this code:

    
    <?php
    include 'name.php';
    $site = 'http://' . $aff_id . '.product.clickbank.net/';
    header( 'Location: $site' ) ;
    ?>
    
    PHP:
    by changing the value of $aff_id on ID.php, I can change the affiliate link on ebook.php. the advantage of this is I can change a single ID.php to affect all php files I had. I used the code as shown above but it doesn't work. can anyone help me?
     
    zenite, Feb 21, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Use double quotes. Variables between single quotes won't be parsed.
    
    header( "Location: $site" );
    
    PHP:
     
    nico_swd, Feb 21, 2008 IP
  3. zenite

    zenite Peon

    Messages:
    240
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, it worked. what a simple solution, lol I didn't manage to solve it myself.
     
    zenite, Feb 21, 2008 IP
  4. fallen

    fallen Well-Known Member

    Messages:
    162
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    118
    #4
    usually variables inside the quotes are leading to errors for new coders, better use string joining like:
    header( "Location: ".$site );
    PHP:
    so you can use whatever quotes you want:
    header( 'Location: '.$site );
    PHP:
    cheers
     
    fallen, Feb 21, 2008 IP
  5. zacharooni

    zacharooni Well-Known Member

    Messages:
    346
    Likes Received:
    20
    Best Answers:
    4
    Trophy Points:
    120
    #5
    Additionally, you should put a die() statement after your header.
     
    zacharooni, Feb 21, 2008 IP
  6. zenite

    zenite Peon

    Messages:
    240
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6

    what does the die func does? I look it up at http://sg2.php.net/die and still have no clue. does it work like return()?
     
    zenite, Feb 21, 2008 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    die() completely ends the PHP script, same as exit(), and doesn't run any line (even regular HTML output below it) afterwards

    return just exits a function/included file and continues the original script
     
    zerxer, Feb 22, 2008 IP