Jump to content

[SOLVED] Track the number of new customers


sungpeng

Recommended Posts

 

<?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?

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.

 

<?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.

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.

<?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.

<?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..

<?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...

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'];

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. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.