Jump to content

Views Counter only add this once each time not refresh.


Gayner

Recommended Posts

Like I have a row in my table called "views'

 

and in my php code

 

When i view my prayer, i want it only increment my views using this code:

 

update table set views=views+1 where id=$id

 

But only once per session for each prayer :-) So people don't spam views up u know ?

Link to comment
Share on other sites

will this work

 

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  mysql_query("UPDATE prays set views=views+1 WHERE id={$row['id']}");
}
else
echo "already viewed";

 

I am sorry but i am so lost on sessions, how'd u get $_SESSION['viewed']

 

can we just create our own names for sessions? lmao

 

cause right now it doesn't update

 

i got over 200 prayers rows how does this work for each one seperately?

Link to comment
Share on other sites

Where does $row['id'] come from ?

 

Think of sessions as a global array.

 

you should have something like this

 

session_start();
if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
}else{
  echo "already viewed";
}

 

pass &debug=true in the URL to override the "already viewed" check

Link to comment
Share on other sites

Where does $row['id'] come from ?

 

Think of sessions as a global array.

 

you should have something like this

 

session_start();
if(!empty($_GET['debug'])) $_SESSION['viewed'] = false; //debug

if(empty($_SESSION['viewed'])){
  $_SESSION['viewed'] = true;
  echo "hey";
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
}else{
  echo "already viewed";
}

 

pass &debug=true in the URL to override the "already viewed" check

 

rowid from my mysql

 


$query="SELECT `id`,title,prayer,time,level,nameid,name,prays FROM prays WHERE id = {$_GET['view']} ORDER by id DESC";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{

 

it's just id from table,

i dont wanna use _GEt to override that will look ugly on the url, i know some sites have in built in..

Link to comment
Share on other sites

Like I have a row in my table called "views'

 

and in my php code

 

When i view my prayer, i want it only increment my views using this code:

 

update table set views=views+1 where id=$id

 

But only once per session for each prayer :-) So people don't spam views up u know ?

 

Lol ur Script didn't even help me people just used &Debug=true to keep spamming.

 

here is the final product, works flawless, reduces 80% of views

 

 

 

// anti flood protection
if(isset($_SESSION['lol']) && $_SESSION['lol'] > time() - 3){
    // users will be redirected to this page if it makes requests faster than 2 seconds
    echo "";
exit;
}
else 
echo "hey"; 
  $SQL = sprintf("UPDATE prays set views=views+1 WHERE id=%d LIMIT 1",$row['id']);
  mysql_query($SQL) or die($SQL."\n".mysql_error());
  mysql_close();
  
$_SESSION['lol'] = time();
}

 

WIN WIN!

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.