www.tutorialspoint.com Forum Index
Register FAQMemberlistUsergroupsTutorials PointLog in
Reply to topic Page 1 of 1
having trouble with cookie coding
Author Message
Reply with quote
Post having trouble with cookie coding 
Ok, mezz here just new and having so much trouble with this coding. I imagine the mistake I'm making is small but I just can't work it out.

I'm a newbie at this so any answers will need to be in baby speak.

What I want:
I want to track a surfer's ID number through my site. They click on a google ad that has an ID in it and come to my page. While on my page, instead of immediately clicking on the offer, they see an article they can read to find more information before they buy and click on the link. I want their ID number to follow them to the article and then back again so that when they do click on my offer I know which ad they arrived from in the first place.

this is where I am.

have this cookie code in the header of my page above <html>

<?php
setcookie($cookie['kid']);
?>

the id is on that page I arrive at.

then in the <body> section I have this:

<?php

if( isset($cookie['kid']) ) {
$sub_id = $cookie['kid'];
}

?>

but when I click on a link the ID doesn't go through. What am I doing wrong?

I do hope I can find someone who can help. I've been trying to find the answer for months. I'm going a little spare Shocked

mezz

View user's profile Send private message
Reply with quote
Post  
Hi Mezz..Try the following

To set a cookie

<?php

setcookie( "Cookie_name", "Cookie_Value" );
?>

Now you can access about cookie as follows:

<?php

$value = $_COOKIE( "Cookie_name" );
if( isset( $value ) )
{
do whatever you like.....
}
?>


Hope this helps.....


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Reply with quote
Post  
so is 'kid' my value or my name? I'm thinking it's my name. And if it is, what's my value? Is sub_id the value?

mezz

View user's profile Send private message
Reply with quote
Post  
Hi Mezz!

I would suggest you to through COOKIES session available on http://www.tutorialspoint.com/php/php_cookies.htm

Your Cookies concepts need more burshup. So please go through this tutorial to understand basics of PHP cookies.


Still you would have question then I will help you.

Thanks


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Reply with quote
Post  
Ok so this is what I did.

I put:

<?php
setcookie( "Cookie_['sub_id']", "Cookie_['kid']" );
?>

in the header

and I put:

<?php
$value = ( "Cookie_['sub_id']" );
if( isset( $kid ) );
{
$sub_id = $cookie['kid'];
}

?>

in the body.

At first I put $value = $COOKIE( "Cookie_['sub_id']") as the first line but I kept getting an error message about unsupported () in that line.

In the URL address line it is still not showing the subID number going across and if it's working it should right?

need more help

(oh and just for some hopefully helpful feedback. Your tute page that I read may make sense to someone who knows what you're talking about but for a greenie like me, I'm still confused. )

mezz

View user's profile Send private message
Reply with quote
Post  
No comments at all????? Sad

View user's profile Send private message
Reply with quote
Post  
Hi Mezz!

Believe me its not too difficult subject to understand. Its very simple.

Assume you want to save a user ID ( foo ) on one page and you want to access this user ID on any other page. Then here is example


Put this code to set cookie

<?php
setcookie( "USER_ID", "foo" );
?>


Now you can access this cookie on any PHP page as follows:

<?php

$value = $_COOKIE( "USER_ID" );
if( isset( $value ) )
{
echo "User ID is ". $value;
}else{
echo "User ID is not set";
}
?>

Please tell me what you are not getting here.

Mohit.


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Reply with quote
Post  
Ok, here's what I've done.....

I copied your setcookie thing

<?php
setcookie( "USER_ID", "foo" );
?>

and changed it to

<?php
setcookie( "USER_ID", "kid" );
?>

saved the page and then went to see if it worked. Everything ok so far.

I then put the next bit in....


<?php

$value = $_COOKIE( "USER_ID" );
if( isset( $value ) )
{
echo "User ID is ". $value;
}else{
echo "User ID is not set";
}
?>


Fatal error: Call to undefined function: array() which is the ( "USER_ID" ) brackets.

So then I removed the brackets and got this message Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

So then I changed it to this

<?php

$value = ($_COOKIE [ "USER_ID" ]);
if( isset( $value ) )
{
echo "User ID is ". $value;
}else{
echo "User ID is not set";
}
?>

Now after saving I go to my page and check it out and just above the page it says "USERID is Kid"

Well, I don't want that up there. I don't think it's supposed to be up there is it?

So anyway, I did a whole heap of other stuff too that I won't get into now coz it'll just be confusing. What do you suggest I do?

mezz

View user's profile Send private message
Reply with quote
Post  
Hi Mezz,

What I understand from your description is that now you are able to set a cookie and you are able access this cookie in another page also.

But I did not understand your sentence when you said it should not be up there...why it should not be up there...once you set a cookie it will remain up till it expired.

Second from your original problem you said you want to track your site visitors who are coming from google ads. So if that is the case then set your cookie in a page where your visitors are landing and then access that cookie in a page where you are taking orders.

Does it help ?

I'm sorry if I'm not understanding your requirement.


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Reply with quote
Post Post Confirmation 
Thanks for your post. This post will be reviewed by webmaster and will be online very soon.!

View user's profile Send private message
Reply with quote
Post  
Hi Mezz!

Its being displayed at the top of your page because you have echoed it using "echo" function. Just remove this function and you won't get it on your page.

Regarding why it is not being recorded, I don't see source code to record it thats why it is not being recorded. You have to write logic to record it. Now I don't know where do you want to record it. So just check what are you doing for other pages, and then do same for this page as well;



Thanks


_________________
Moderator, TP
Keep visiting and share this site with your friends.
View user's profile Send private message Send e-mail
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum