PHP and cookies - is this possible? Please read!

Discussion in 'PHP' started by heygeek, Sep 25, 2007.

  1. #1
    Hi,
    OK, so here's what I want to do:

    I have a website with several different products for sale on it. I promote the site using PPC. I want to be able to run one PPC ad campaign for the site with normal pricing (let's say each product is $200), and another campaign for the site with sale pricing (sale price of each item is $100). I want to be able to put a cookie on the users computer when they click on the PPC ads for the sale price it displays the sale prices instead of the normal prices.

    This should be fairly simple, right?

    I know a little PHP and am good with HTML, as I understand it is should function sort of like this:

    User clicks on ad for sale price, gets directed to landing page that puts cookie on their computer.

    User browses website, wherever there is mention of a price the site checks to see if the user has the cookie, if they do the sale price is shown, if not then the regular price is shown.

    Does anyone know of a simple tutorial that would show how to do this? I found a bunch of tutorials showing how to set up the cookie, but none that showed how to get the page to display different messages (prices) based on the cookie being present or not.

    If anyone can help I would really appreciate it!

    Thanks,
    George
     
    heygeek, Sep 25, 2007 IP
  2. angielski

    angielski Peon

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    page making cookie on user's computer:

    setcookie("bonus", "lower_price");

    The page where price is shown:

    You can buy it for <?php
    if ($HTTP_COOKIE_VARS["bonus"]) echo "100$";else echo "200$";
    ?>
     
    angielski, Sep 25, 2007 IP
  3. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #3
    like he said but


     
    Meth_, Sep 26, 2007 IP
  4. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4

    You don't actually need brackets on if statement in PHP if the statement is only one line. You only need them if it's more than one line.

    Saves a bit of time when writing code so it's useful. :D

    Sky22
     
    sky22, Sep 26, 2007 IP
  5. gsv13

    gsv13 Well-Known Member

    Messages:
    2,773
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    130
    #5
    sky22 is right
     
    gsv13, Sep 26, 2007 IP
  6. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would suggest using sessions rather than cookies , some people have restrictive cookie settings. If you are using PHP on ur site then it shouldnt be a problem.

    on top of each page OR in ur top level include file

    session_start();
    if(user is coming from PPC){
    $_SESSION['PPC'] = 1;
    }

    to check

    if($_SESSION['PPC'] == 1){
    echo "Price : 100";
    }else{
    echo "Price : 200";
    }
     
    sabmalik, Sep 27, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Yes, but at least he got rid of the deprecated HTTP_*_VARS

    Also, when using one line conditionals, either use the ternary operating or brackets like Meth done to improve readability.

    I prefer:
    echo ($_SESSION['bonus'] ? '$100' : '$200');
    Code (markup):
    Note the session used (just realised sabmalik already covered this :))
     
    krt, Sep 27, 2007 IP
  8. sabmalik

    sabmalik Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Let me see how you guys are getting good looking code :)

    
    session_start();
    if(user is coming from PPC){
       $_SESSION['PPC'] = 1;
    }
    
    
    ///to check
    if($_SESSION['PPC'] == 1){
        echo "Price : 100";
    }else{
        echo "Price : 200";
    }
    
    Code (markup):
     
    sabmalik, Sep 27, 2007 IP