Dynadot

[Resolved] PHP and cookies - Is this possible? Please read!

Spaceship Spaceship
Watch
Impact
8
PHP and cookies - is this possible? Please read!

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
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
setcookie: http://php.net/setcookie
Get a cookie with $_COOKIE or $_COOKIE['name']

Just remember, if I edit my cookie to 1, it might sell the $3000 product to me for 1 dollar.
 
0
•••
HeyGeek said:
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.
First of all, yes it is certainly possible, but I guess you are already aware of that :).

I am sorry but I cannot provide you with an actual tutorial, but the process of the Cookie is rather simple. First you need to set it, on your landing page, with setcookie(). Then, on each page you are displaying prices on, you need to check whether this Cookie is set (via the array $_COOKIE) and need to adapt the price.
 
0
•••
In PHP you can use something like:

Code:
if ($_COOKIE['discount'] == $discount_code)
{
   $price = $discounted_price;
}
else
{
   $price = $full_price;
}
 
0
•••
Overall, a user might do this.

Goes to site, sees a cookie being added for $40.
User opens cookies, changes $40 to $5
User buys product for $5.

Even if you don't do this, what's to stop every user using it anyway? Money off is very desirable. :hehe:

You'll need to add some verification remember.

Dan

P.S. neroux, beat you. :p
 
0
•••
Danltn said:
Overall, a user might do this.

Goes to site, sees a cookie being added for $40.
User opens cookies, changes $40 to $5
User buys product for $5.

You'll need to add some verification remember.

Dan
Thats correct, but I'd never advise to store price information in the Cookie, only a flag determining which price to show.

Danltn said:
P.S. neroux, beat you. :p
But mine is longer - the text :D
 
0
•••
Never pass pricing info in a cookie. Just pass a variable that indicates whether the sale price / discount is given. If so, then look up this value in your database and display the discounted price.

You can use the setcookie() function.

FYI - if a person has cookies turned off, they will not get the sale price. I have switched from using cookies to using session variables. A surfer cannot turn off session variables.

Code:
session_start();
session_register('discount');
-Bob
 
0
•••
Wow, that was alot of great responses! Thanks!

As far as storing prices in the cookie, I wasn't planning on doing that, the idea is to simply set a cookie, then wherever there is mention of a price on the site to use code that does the following every time I have mention of a price on a page:

CHECK: is the cookie present?
IF IT IS: display: (the html for the $99 sale image or text)
IF IT IS NOT: display: (the normal price or image)

Is there a simple code snippet for doing that? I would need to use it multiple times on the same page as I would have sale prices for more then one item.

@Danltn: the sale landing page would only be accessed through certain ads and links, so the majority of the visitors won't even know about it.

Thanks!
 
0
•••
moondog said:
FYI - if a person has cookies turned off, they will not get the sale price. I have switched from using cookies to using session variables. A surfer cannot turn off session variables.
Only, if your PHP installation is configured to rewrite URLs transparently (session.use_trans_sid), which defaults to no however.

Also, sessions would allow only a temporary discount. Once the user closes his browser, the discount is gone.

HeyGeek said:
Is there a simple code snippet for doing that? I would need to use it multiple times on the same page as I would have sale prices for more then one item.
Please refer to the code samples (setcookie() and $_COOKIE) shown here. For more detailed examples one would need to know your code.
 
0
•••
HeyGeek said:
Wow, that was alot of great responses! Thanks!

As far as storing prices in the cookie, I wasn't planning on doing that, the idea is to simply set a cookie, then wherever there is mention of a price on the site to use code that does the following every time I have mention of a price on a page:

CHECK: is the cookie present?
IF IT IS: display: (the html for the $99 sale image or text)
IF IT IS NOT: display: (the normal price or image)

Is there a simple code snippet for doing that? I would need to use it multiple times on the same page as I would have sale prices for more then one item.

@Danltn: the sale landing page would only be accessed through certain ads and links, so the majority of the visitors won't even know about it.

Thanks!

Jido got it spot on pretty much.

PHP:
if ($_COOKIE['discount'] == $discount_code)
{
   $price = $discounted_price;
   // Add anything else you need here, HTML output
}
else
{
   $price = $full_price;
   // Add anything else you need here, HTML output
}

If their cookie is the same as the discount coupon, then it'll do the top one, otherwise the bottom one.
 
0
•••
EDIT:
DANLTN: didn't see your post till I had posted this, I'll mess around with that. Thanks.
-----
Here is the website I am using it on: http://no_url_shorteners/ysuhrn

As you can see, the website is currently HTML, all I want to do is define which images are showed (sale price or regular price) on this and other pages. There is really no existing code.
 
Last edited:
0
•••
HeyGeek said:
CHECK: is the cookie present?
IF IT IS: display: (the html for the $99 sale image or text)
IF IT IS NOT: display: (the normal price or image)

Is there a simple code snippet for doing that?
I think that my example covers this quite well, you just have to display the price in question. Do you fetch prices from a database? Then you can change the select statement according to the cookie being there or not.
 
0
•••
OK, so here is what I have so far, for some reason it is displaying the "sale price" message regardless of wether I have the cookie present or not:

Landing page that places the cookie:

<?php
session_start(); // this needs to be declared on the top of every page, specially before sending any output to the browser.
?>

<?php
setcookie("sale", "active", time()+3600,"/","MY SITE - GEORGE");
?>


Page that calls up the cookie:

<?php
session_start(); // this needs to be declared on the top of every page, specially before sending any output to the browser.

?>

<?php
if ($_COOKIE['sale'] == $active)
{
echo ' sale price ';
}
else
{
echo ' full price ';
}
?>


Am I missing something obvious?
 
0
•••
0
•••
HURRAY! I think I figured it out! Thanks guys!
 
Last edited:
0
•••
moondog said:
FYI - if a person has cookies turned off, they will not get the sale price. I have switched from using cookies to using session variables. A surfer cannot turn off session variables.

Code:
session_start();
session_register('discount');
-Bob

Just a back track a bit. If you use sessions then you could still be using cookies (although the session id is the only thing held within it). Sessions are tracked either of 2 ways:-

1) Through a session ID that is added to the url's on your page.
2) The session id is stored in a cookie.

1 has the advantage of working on almost all browsers if you do not check the authenticity of the user when someone visits an address with a session id however you could have the wrong person (they could have given the url to a friend for example). 2 has the advantage of keeping your address clean cookies have a similar risk of the first option but the person would steal the cookie data instead (is sent through standard headers, the risk is greatest on pc's that multiple users have access too).

Which method is used depends on several things:-

1) The settings of the browser (if they accept cookies)
2) The settings in php.ini
3) Settings set within a script
 
0
•••
moondog said:
Never pass pricing info in a cookie. Just pass a variable that indicates whether the sale price / discount is given. If so, then look up this value in your database and display the discounted price.

You can use the setcookie() function.

FYI - if a person has cookies turned off, they will not get the sale price. I have switched from using cookies to using session variables. A surfer cannot turn off session variables.

Code:
session_start();
session_register('discount');
-Bob
Just a note... do not use session_register! It is deprecated code and is for register_globals.

Note from php.net:
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back