sungpeng Posted April 14, 2009 Share Posted April 14, 2009 <?php include 'config2.php'; $counted=setcookie("user", "mydomain", time()+60*60*60); if($counted==""){ setcookie("counter","counted",time()+60*60*60); mysql_query("UPDATE stats SET counter=counter+1 where id='0'"); } The above code intended to track the number of new customers visiting my webpages. It is not working, can anyone help to correct it? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 why not log their IP? then just COUNT(*) the entries? and nice double post Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 hi masterace, can draft the code for me to see? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 I need it to save a cookie on my customer computer. So the next time if there is a cookie in that computer, it will not activate the code. IP address change every time you "on" the computer, so I think it not workable to save cookie using IP address. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 <?php $ip = $_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT * FROM `visitors` WHERE `ip`='".$ip."' LIMIT 1") or die(mysql_error()); if(mysql_num_rows($query) == 0) { mysql_query("INSERT INTO `visitors` (ip) VALUES('".$ip."')") or die(mysql_error()); } else { echo "This user has already visited your website before!"; } ?> you can put both into the 1 script. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 hi master, the above code will work only if IP address is always the same. As I know, IP address will change every time a person close and on the computer. So I don't think it is workable. It is better to save a cookies in customer computer and call out the cookies once that customer visit my website again so that the code will not be activated. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 if they clear their cookies then that won't work anyway. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 People seldom clear their cookies. If they do so, all the websites that they visit need to rekey the username and password again. That is troublesome. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 Can help draft a cookies code? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 to be honest, I've never worked with cookies before lol. Never had a genuine use for them. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 Anyway Thank master. My first top post provide a starting code but not working. Can anyone help edit it? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 <?php $counted=setcookie("user", "mydomain", time()+60*60*60); if($counted==""){ $counted is just setting a cookie, you need to change that to selecting a already set cookie. Otherwise the 'if' statement is never executed. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 <?php $link = mysql_connect('localhost', 'write_here_mysql_username', ' write_here_mysql_password'); mysql_select_database('write_here_mysql_database_name',$link); $counted = $HTTP_COOKIE_VARS["counter"]; if($counted==""){ setcookie("counter","counted",time()+60*60*60); mysql_query("UPDATE stats SET counter=counter+1"); } $query=mysql_query("SELECT * FROM stats"); while($row=mysql_fetch_assoc($query)) { $stats=$row['counter']; } ?> Found this code, it is not working also.. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 <?php include 'config2.php'; $counted = $HTTP_COOKIE_VARS["counter"]; if($counted==""){ setcookie("counter","counted",time()+60*60*60); mysql_query("UPDATE stats SET counter=counter+1 where id='0'"); } ?> Make it more simple.. It always come out an error message Warning: Cannot modify header information - headers already sent by... Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 Yes, cookies are available in a superglobal $_COOKIE[]. I'm not clear what your scheme is, but as cookies are name value pairs, whatever cookie you've set can be referenced by checking it's name ie. echo $_COOKIE['foo']; Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2009 Share Posted April 14, 2009 Cookies are set in the HTTP header, so once the header is sent, you'll get that error. Something is causing your HTTP response to be sent. I think there's a sticky on the top of the forum about this, so I won't go into the reasons or the solution here. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 I need it to disable counting same visitors every time he visit my page. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 if($_cookie["counter"]==""){ setcookie("counter","counted",time()+60*60*60); mysql_query("UPDATE stats SET counter=counter+1 where id='0'"); } I still get error message Warning modify header information - headers already sent by .. Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 <?php setcookie("user", "Alex Porter", time()+3600); ?> Check why simple cookie form always give an error Warning: cannot modify header information - header already sent by.. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 is this... <?php setcookie("user", "Alex Porter", time()+3600); ?> after any html output ? Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 ya I think I put inside html. I should put before. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 I should put before. yep Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 <?php echo "hello"; setcookie("user", "Alex Porter", time()+3600); ?> if I put something before setcookie. It will come out error Warning cannot modify header information.... Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 yep that's right, setcookie(); needs to go before everything else. Same as session_start(); Quote Link to comment Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 <?php if (isset($_COOKIE["user"])){ echo "Welcome " . $_COOKIE["user"] . "!<br />"; }else{ echo "Welcome guest!<br />"; setcookie("user", "Alex Porter", time()+3600); } ?> how can I put a "if" function before? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.