Pasing URL Parameters

Discussion in 'PHP' started by imarketing101, Jan 5, 2009.

  1. #1
    Hi!

    I need to pass some parameters on my site and I'm not quite sure how to do this.

    Say, someone comes to my site with an affiliate ID:
    [http://www.mysite.com?aff_id=AFFILIATE

    I wanted to be able to:
    1. set a cookie for this, so that if they come again some other day my affiliate still gets paid.
    2. Hide the URL, so http://www.mysite.com?aff_id=AFFILIATE becomes just http://www.mysite.com after the link is clicked
    3. Pass the parameter to the order page so that at the bottom of my site I could have a link with: <a href="https://somecheckoutsystem.com?product=1&aff_id=AFFILIATE> Order Now</a>

    How do I do this using php?

    Thanks!
     
    imarketing101, Jan 5, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    $id = intval($_GET['aff_id']);
    $days_to_keep = 30;
    $domain = 'domain.com'; // Example
    
    if(!headers_sent($file, $line)) {
        setcookie  ( 'aff_id' , $id , time() + $days_to_keep * 86400 , '/' , $domain , false , true /* avoid XSS */ );
        header('Location: http://' . $domain);
        die('<a href="http://' . $domain . '">Continue</a>');
    }
    else
        die('Headers already sent in ' . $file . ' - Line: ' . $line . PHP_EOL);
    
    PHP:
    echo $your_link . '&amp;aff_id=' . intval($_COOKIE['aff_id']);
    PHP:
    Completely untested/untried, just wrote in the box now.
     
    Danltn, Jan 5, 2009 IP
  3. imarketing101

    imarketing101 Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok, so I take it I put the first part of the code on top of the page. But what do I do with the second part?

    How do I make it so I can get the parameter from that cookie anytime and write it in the the link (or redirect page)?
     
    imarketing101, Jan 5, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    echo $_COOKIE['aff_id'];

    Should echo the aff_id stored at any time.

    echo isset($_COOKIE['aff_id']) ? intval($_COOKIE['aff_id']) : '';

    Is probably preferably however to avoid warnings/attacks.
     
    Danltn, Jan 5, 2009 IP
  5. imarketing101

    imarketing101 Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tested this code and it didn't work... suggestions?
     
    imarketing101, Jan 5, 2009 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Is aff_id a number, a string or what?
     
    Danltn, Jan 5, 2009 IP
  7. rags02

    rags02 Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    This worked. In IE, the cookie wasn't showing as the correct value until the page was refreshed which could be fixed with a simple javascript function.

    On the initial landing page:
    <?php
    /* 
     * For add'l help on setting the cookie and applicable params: 
     * http://us.php.net/manual/en/function.setcookie.php 
     */
    $cookie_path = '/';		// path for storing the cookies... the cookie jar
    $cookie_domain = '';	// limits the domain in which the cookie is 'edible'
    // reset the affiliate id cookie each time... comment this out if you don't want to
    setcookie("affiliate_id", "", time() - 99999999, $cookie_path, $cookie_domain, false, true);
    
    // hello affiliate id (through url)
    $aff_id = $_GET['aff_id'];
    
    // cookie: desired life
    $cookie_life = 30; // days
    
    // make sure we've got an aff_id and that headers haven't been sent yet (so user can be redirected)
    if (!headers_sent() && isset($aff_id)) {
    	
    	$expires = time() + (86400 * $cookie_life);							// when the cookie expires
    	
    	// set the cookie (PHP 5.2.0)
    	setcookie("affiliate_id", "$aff_id", $expires, $cookie_path, $cookie_domain, false, true);
    	// comment out for less than PHP 5.2 setcookie("aff_id", $aff_id, $expires, "$cookie_path");
    	
    	//redirect the page
    	$redirect = 'http://localhost/brain/digitalpoint/';	// where we end up (ie: mysite.com)
    	header("Location: $redirect");
    }
    
    ?>
    PHP:
    Then on the redirect page:
    <?php
    // hello cookie
    $aff_id = $_COOKIE['affiliate_id'];
    // the link with the affiliate id as a url variable
    $link = '<a href="https://somecheckoutsystem.com?product=1&aff_id=' . $aff_id . '">Order Now</a>';
    // show it
    echo $link;
    ?>
    PHP:
    It's a little rough... but it's something
     
    rags02, Jan 5, 2009 IP
  8. eric_basher

    eric_basher Peon

    Messages:
    185
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    try using session variable instead..
     
    eric_basher, Jan 5, 2009 IP
  9. rags02

    rags02 Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    I agree... just don't know if they already have a site with a Cookies based system... if they don't mind a little inconsistency in session data handling, that could be an easy fix.
     
    rags02, Jan 5, 2009 IP
  10. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I suggest you to use header(location:) , after getting the affiliate refer. After getting the visitor, link/add will get neccessary information and then redirect to link. You're hiding it in few seconds.
     
    mdrobiul, Jan 6, 2009 IP
  11. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hold the affiliate refer information with SESSION and keep it in your all pages until you need to work with it. DANLtn wrote nice script.
     
    mdrobiul, Jan 6, 2009 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    "so that if they come again some other day my affiliate still gets paid."

    Sessions wouldn't work for this, you'd need to set a cookie at least to try and record this, but yes - I would recommend a session for the short term, which is then logged in a DB.
     
    Danltn, Jan 6, 2009 IP
  13. rags02

    rags02 Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #13
    since some users delete history/cookies on browser close, the cookie for remembering them if they come back in order to pay the affiliate would be moot. by any chance do they register / login? you could log it that way through a DB.

    also for the previous cookie script, changing:
    //redirect the page
    $redirect = 'http://example.com'; // where we end up
    header("Location: $redirect");
    PHP:
    to:
    //redirect the page
    $redirect = 'http://example.com'; // where we end up
    header( "refresh: 0; url=$redirect" );
    PHP:
    ... fixes the IE issue I mentioned earlier.
     
    rags02, Jan 6, 2009 IP