Page 1 of 1

how would i do this?

Posted: Wed Dec 31, 2003 5:55 am
by lizlazloz
how would i make it so that a page can only be viewed once a day by a user?

i would get the ip addresss...

then how would i check it had not visited this page before today? adding all ips that visit the page into a MySQL table then checking cant eb the best way, surely?

Posted: Wed Dec 31, 2003 5:59 am
by AVATAr
cookies. or using user/pass

if you use ip address you will block people that uses the same ip (corporate users... etc..)

Posted: Wed Dec 31, 2003 6:01 am
by lizlazloz
ah, cookies, good idea. people could delete them... but hey, they'll do for the monent.

Posted: Wed Dec 31, 2003 10:38 am
by lizlazloz
ok well so far I've got this:

Code: Select all

<?php 
if( ! $_COOKIE("link") == "ok" ) 
{ 
$conn=mysql_connect("localhost","shaun","squall") 
or die("Could not connect"); 

$rs = mysql_select_db("game", $conn) 
or die("couldnt select database"); 

$sql="update game set strength= strength + 1 where id=1"; 

$rs=mysql_query($sql,$conn) 
or die("could not execute query"); 

$sql="select id,name,strength from game where id=1"; 

$rs1=mysql_query($sql,$conn) 
or die("could not execute query"); 

while( $row = mysql_fetch_array($rs1) ) 
{ 
$msg="You just clicked <b>".$row[name]."'s</b> secret link!  "  .$row[name]."'s strength has  increased to <b>".$row[strength]."</b>"; 

} 
setcookie("link","ok", time()+86400 ); 
} 
else 
{ 
$msg="You can only click this link once a day!"; 
} 
?> 
<html><head><title>Page</title></head> 
<body> 
<?php 
echo($msg); 
?> 
</body></html>
and i get " Array to string conversion on line 2" (this line: if( ! $_COOKIE("link") == "ok" ) )

why? whats wrong with my code?

Posted: Wed Dec 31, 2003 12:22 pm
by m3mn0n
Use a database to record each IP/date/time/browser and possibly use [php_man]sessions[/php_man] aswell.

Sessions have an edge over cookies in this case, because it's better that the webmaster controls the information rather than the client to prevent double accessing.

Also that line is wrong because arrays use square brackets not parenthesis. Make it $_COOKIE['link'].