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? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/ 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 Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809208 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? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809213 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809220 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809222 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809225 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809227 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809229 Share on other sites More sharing options...
sungpeng Posted April 14, 2009 Author Share Posted April 14, 2009 Can help draft a cookies code? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809230 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809231 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? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809235 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809241 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.. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809250 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... Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809252 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']; Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809257 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809261 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809269 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 .. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809270 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.. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809285 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 ? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809286 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. Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809292 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 I should put before. yep Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809293 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.... Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809302 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(); Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809305 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? Link to comment https://forums.phpfreaks.com/topic/153967-solved-track-the-number-of-new-customers/#findComment-809309 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.