...

View Full Version : Stop counter refresh



mariodac
12-02-2010, 12:57 AM
OK, my first post here...very newbie at PHP – so, please be Gentle…:D
Trying to see if anyone could help with the following problem, I have searched, but really did not find anything that would help me solve this… if it’s out there I completely missed it.:rolleyes:

I have a script that has numerous pages (ads) each has a counter that updates in a MySQL DB with the number of times the page has been viewed. Currently every time the page is reloaded then the count increments by one. Obviously that brings up the problem of the page just getting reloaded to
increase the number of hits so I want it to check the session so that it would update only once even if the same person refreshed the page several times.

So here is the part of the script that updates the DB


$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
$result1=q($s);

Below is what I managed to put together, it works. When the user goes from one page (ad) to another it increases the counter by one, if the user returns to a previously visited page it does not. But it’s not exactly the way I want it:


if(!isset($_SESSION['page'])){ $_SESSION['page'][] = $_SERVER['REQUEST_URI']; }

if(!in_array($_SERVER['REQUEST_URI'], $_SESSION['page'])){


$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
//$result1=q($s);

$_SESSION['page'][] = $_SERVER['REQUEST_URI'];
}
How do I set it up so that the SESSION will expire after a certain amount of time? Because currently if the user returns (which a lot of users do) to the site the following day, the counter does not update any pages he/she previously visited.

Thank you very much for any help anyone can provide

Fumigator
12-02-2010, 04:02 AM
Cookies are more in line with what you'd like-- a storage area that expires after an amount of time. That pretty much describes a cookie exactly.

If you are adverse to cookies (which you shouldn't be, if you're using sessions, since PHP sessions use the cookie system by default), then you can create a session variable that stores a timestamp at the same time you set your session, and then add a check of that timestamp when you find the session. If it's old enough, update the counter and reset the timestamp.

Lamped
12-02-2010, 09:14 AM
You should take a look at the post I made on http://codingforums.com/showthread.php?t=209538

This gives an example of IP based counters.

mariodac
12-02-2010, 01:22 PM
Cookies are more in line with what you'd like-- a storage area that expires after an amount of time. That pretty much describes a cookie exactly.

If you are adverse to cookies (which you shouldn't be, if you're using sessions, since PHP sessions use the cookie system by default), then you can create a session variable that stores a timestamp at the same time you set your session, and then add a check of that timestamp when you find the session. If it's old enough, update the counter and reset the timestamp.

Fumigator
Would you be willing to help me out with the code for the cookies? :confused:
I have tried some, but for some reason could not get it to work
Thank you for your help

Fumigator
12-02-2010, 02:54 PM
I can help you out with your cookies, sure. They are pretty straight forward; just don't try to use a cookie you've set until the page is reloaded.

mariodac
12-02-2010, 04:08 PM
I can help you out with your cookies, sure. They are pretty straight forward; just don't try to use a cookie you've set until the page is reloaded.

I really appreciate the help
here is my code that I need to include

$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid";
$result1=q($s);

Thanks again

Fumigator
12-02-2010, 04:51 PM
Where's your cookie code?

mariodac
12-02-2010, 07:59 PM
That is the thing, I'm not sure where to start
Currently there's no cookie in the page.
As I said, I'm pretty new at PHP. Trying to learn by trial and error

Let me know if you can still help me out
Thank you

Fumigator
12-02-2010, 09:04 PM
Alright well, start with google, "php cookie tutorial" should get you enough info to dig in. The idea is to use the superglobal $_COOKIE array.

I also found the PHP manual to have a very good description of cookies, but some people find manuals difficult to use for some reason. Http://php.net

mariodac
12-02-2010, 10:01 PM
The part that confuses me is this
The page that I need to insert the cookie is a Dynamic page.
So when the user visits another AD page (same detail.php file)

How is that going to work? It sees the same cookie so the counter will not update.

Sorry if this is turning to be too much of a pain...:(

Fumigator
12-02-2010, 10:12 PM
Sounds like you want a new cookie for each dynamically loaded page. If you name the cookies using some unique page identifier, that'll work fine.

mariodac
12-03-2010, 03:39 AM
Ok
I did some searching around and found some similar code I think would work.
I implemented it with a few minor changes to fit my code.
It does work, except when the user goes to another Dynamic loaded page, it will not count until the time specified has expired.

How do I make the following code apply to dynamically loaded pages

For example: User visits page X the counter increases
User visits page Y the counter increases (this is not happening with the current code displayed below)
User visits page X before the time set expires and the counter DOES NOT increase -- The counter will only increase after the set time has expired



$minvar = 10; //minutes before counter increments resume.

$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid"; //original code

if(!$_SESSION['page']) {
$_SESSION['page'] = time();
$result1=q($s) or die(mysql_error());

}
elseif(($_SESSION['page'] + ($minvar*60)) < time()) {
$result1=q($s) or die(mysql_error()); // starts recounting after the time set expires
$_SESSION['page'] = time();
}

I could not figure out how to set cookies for each dynamically loaded page

Any suggestions to fix this code?

Thank you

Fumigator
12-03-2010, 06:36 AM
It's really simple, you need to store a separate session variable, or cookie, or whatever, for every dynamically created page.

mariodac
12-03-2010, 01:36 PM
OK
I have no clue on how to get that done
I have tried it, but no luck

Can you give me an example?

Thanks

Fumigator
12-03-2010, 07:13 PM
I noticed you have a site ID. Use that for your $_SESSION index name instead of "page".



$_SESSION[$siteid] = time();

mariodac
12-03-2010, 09:38 PM
You mean like this?

$minvar = 10; //minutes before counter increments resume.

$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid"; //original code

if (!isset($_SESSION['page'])) {
$_SESSION[$siteid] = time();
$result1=q($s) or die(mysql_error());
}
elseif(($_SESSION['page'] + ($minvar*60)) < time()) {
$result1=q($s) or die(mysql_error()); // starts recounting after the time set expires
$_SESSION['page'] = time();
}

Still acting the same way. It works on the first page I visit (detail.php?siteid=155) when I go to another page (detail.php?siteid=128) within the 10 minutes the counter does not increase. After 10 minutes the session expires, but once you go to any of the (detail.php?siteid=xxx) dynamic pages it starts the session and same story...
Well at least the session is expiring, now if I could get it to work accross multiple dynamic pages. Any other suggestions???
Thanks again for taking the time to guide me in the right direction.

Fumigator
12-03-2010, 10:23 PM
Take a closer look at your code, you'll find references to the variable $_SESSION['page'] in more than one place.

mariodac
12-03-2010, 10:46 PM
Well I had tried that as shown below and the counter just keeps increasing with each refresh

$minvar = 10; //minutes before counter increments resume.

$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid"; //original code

if (!isset($_SESSION[$siteid])) {
$_SESSION[$siteid] = time();
$result1=q($s) or die(mysql_error());
}
elseif(($_SESSION[$siteid] + ($minvar*60)) < time()) {
$result1=q($s) or die(mysql_error()); // starts recounting after the time set expires
$_SESSION[$siteid] = time();
}



Do I still have it wrong???

Fumigator
12-03-2010, 11:55 PM
Your code's first order of business is to increment the counter and update the table. That stuff needs to be inside the "if" condition.

mariodac
12-04-2010, 01:25 AM
Ok I see.
Just tried it as displayed below -- No Joy
What else do you suggest I try?

$minvar = 10; //minutes before counter increments resume.


if (!isset($_SESSION[$siteid])) {
$_SESSION[$siteid] = time();
$tell=$sitehits+1;
$s = "UPDATE $ads_tbl set sitehits=$tell,datestamp='$datestamp' where siteid=$siteid"; //original code
$result1=q($s) or die(mysql_error());
}
elseif(($_SESSION[$siteid] + ($minvar*60)) < time()) {
$result1=q($s) or die(mysql_error()); // starts recounting after the time set expires
$_SESSION[$siteid] = time();
}

nomorerack
12-04-2010, 01:32 AM
I'm a newbie too...what's next?

Fumigator
12-04-2010, 04:01 AM
FYI the "elseif" block does not assign anything to variable $s but you then use variable $s in your function q()... that won't work.

Also quick question, are you starting a session at the beginning of your script using the session_start() function?

After making some changes, before you run back here to tell me it still doesn't work, try echoing the $_SESSION array and see if it contains what you think it contains. A handy way to echo an array is echo "<pre>".print_r($_SESSION,true)."</pre>";. Add echos to every part of the code you are confused about so you can follow the code through.

mariodac
12-04-2010, 05:44 AM
Fumigator
I really appreciate the help

I have bitten more than I can handle here
As I said, I'm ver [very] new at PHP and I was under the impression I could figure this out.

I'll just have to ask for solutions to this problem and see if anyone can help me out on this instead of me trying to troubleshoot in the "dark".


Also quick question, are you starting a session at the beginning of your script using the session_start() function?

Yes the page starts with

<?
session_start();

Thanks for all the time and effort

zac1987
04-17-2011, 08:24 AM
@mariodac, currently I need what you needed. LOL. I don't think it is a good idea to use cookies because some people browser block cookies. (I not sure about it, I just don't want to take the risk). I am trying to use sessions fully.

I guess you can do like this, steps :

if (!isset($CurrentDatetime4count) OR ($_session[pageurl] != $currentpageurl)) {
$count = $count + 1;
$CurrentDatetime4count = time();
$_session[pageurl] = $currentpageurl;
} elseif (($CurrentDatetime4count - time() > 10 mins) AND ($_session[pageurl] == $currentpageurl)) {
$count = $count + 1;
$CurrentDatetime4count = time();
}

Lamped
04-17-2011, 03:42 PM
Nice bump, but while we're here...

Sessions are either hacked into the URL (discouraged and disabled by default now) or... a cookie.

zac1987
04-18-2011, 02:24 AM
Nice bump, but while we're here...

Sessions are either hacked into the URL (discouraged and disabled by default now) or... a cookie.

omg, i never knew it. How to hack session? To hack cookies, we just need to change the value of cookies from our computer right? For example, change my cookies username zac1987 to michale222.

Updated : I know how to hack session already, info at http://phpsec.org/projects/guide/4.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum