Jump to content

page views...


subhomoy

Recommended Posts

hii i want to create a script where i want to store unique page views per 24 hrs using cookie..

i had create a code but rectify me if i'm wrong...

1) i wan to store a cookie so i set this code on top to create a cookie on views browser..

<php

setcookie("user", time()+84600);

?>

2) Now i want to find that if the cookie was set before, then the views will not update, if not then it will increase by 1.

<php

if  (isset($_cookie["user"]))

echo "Total no of views" . $views  . ";

else

echo" $views=$views+1;

echo " $views";

$k=mysql_query("UPDATE `images` SET views = '$views' WHERE id = '$userid'")

?>

 

but the problem i'm facing is that where to put that "$views" so that it will increase and will show to the user...

 

now suppose every thing works well then how the cookie will understand that the views will be updated in specific users database...

 

Plz guys i really need help... 

 

If some other possible ways are there then plz refer...

 

Thanks in advance..

Link to comment
Share on other sites

Currently, your cookie is only considered a "session cookie". That is, it will expire as soon as you close the browser. Since 24 hours probably won't have passed before the user next opens their browser, this is not acceptable. You'll need to tell the cookie to expire in 24 hours. You have the right idea, but you put it in the wrong parameter. The expire time is the third parameter. So just change it to:

setcookie("user", "", time()+84600);

 

To update the views, this should do:

$k=mysql_query("UPDATE `images` SET views = views+1 WHERE id = '$userid'");

 

I don't know where/how $userid is defined, but make sure it can't contain SQL injection. If it is numerical you can simply cast it to an int to make sure.

$userid = (int) $userid;

Link to comment
Share on other sites

Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also.

 

Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what?

Link to comment
Share on other sites

Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also.

 

Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what?

 

The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view.

Link to comment
Share on other sites

Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also.

 

Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what?

 

The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view.

 

How would you differentiate between each post/image they're viewing?

 

I am trying to do the same thing for  a system of mine.

 

If a user has viewed the topic in less then 24hours, store the id's in a array, and if it exists, echo NO SQL to update+view,  would that be the same case here?

Link to comment
Share on other sites

Whats the value of the cookie user? Im lost, I am actually doing something similiar to this also.

 

Are you going to be storing them id's of the images in the user cookie and check fi they already visited or what?

 

The cookie has no value, it simply exists. Basically if it exists, don't add a view, if it doesn't exist create it and add a view.

 

How would you differentiate between each post/image they're viewing?

 

Right, I guess I was just thinking of the website itself and not individual pages.

 

I am trying to do the same thing for  a system of mine.

 

If a user has viewed the topic in less then 24hours, store the id's in a array, and if it exists, echo NO SQL to update+view,  would that be the same case here?

 

There's a lot of different ways to do it, but most of them result in unnecessary database reads/writes.

 

For something simple like viewing a page and adding a view every 24 hours, it's pretty easy. You can just keep an array in a cookie or something with a last_visited timestamp. For something like a forum where it becomes "unread" after other activities, it's a little more complicated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.