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?

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

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