php如何记录访问者ip到数据库?
<?php
if($_POST["tem"]){
$tem =$_POST["tem"];
$ip =$_SERVER["REMOTE_ADDR"]; //是不是这样获取ip的?
//链接数据库
$server =mysql_connect("localhost","root","123");
$connection =mysql_select_db("laji",$server); //如果laji表中有该访问者的ip,则update表中的tem值;
//如果laji表中不存在访问者的ip,怎插入一条新的;
}
?><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function record(){
$(".test").click(function(e){
$.ajax({
type:"POST",
url:"laji.php",
data:{
tem:$(this).html()
}
});
});
}
$(function(){record();});
</script>
<a href="#" class="test">csdn</a>
<a href="#" class="test">php100</a>
<a href="#" class="test">phpchina</a>
<a href="#" class="test">blueidea</a>

解决方案 »

  1.   


    if(getenv("HTTP_CLIENT_IP")){
    $onlineip = getenv("HTTP_CLIENT_IP");
    } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
    $onlineip = getenv("HTTP_X_FORWARDED_FOR");
    } else {
    $onlineip = $_SERVER["REMOTE_ADDR"];
    }
      

  2.   

    下面的代码为什么没有成功,数据库里面没有任何变化。
    <?php
    if($_POST["tem"]){
    $tem =$_POST["tem"];
    $ip =$_POST["ip"];
    //链接数据库
    $server =mysql_connect("localhost","root","123");
    $connection =mysql_select_db("laji",$server); $sql="select * from record where ip=$ip";
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    if(empty($row)){
    echo "empty";
    mysql_query("insert into record (ip,tem) values ($ip,$tem)");
    }else{
    echo "not empty";
    mysql_query("update record set tem=$tem where ip=$ip");
    }
    }
    $ip=$_SERVER["REMOTE_ADDR"];
    ?><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    function record(){
    $(".test").click(function(e){
    $.ajax({
    type:"POST",
    url:"laji.php",
    data:{
    tem:$(this).html(),
    ip:$("#ip").val()
    }
    });
    });
    }
    $(function(){record();});
    </script>
    <input type="hidden" name="ip" id="ip" value="<?php echo $ip ?>" />
    <a href="#" class="test">csdn</a>
    <a href="#" class="test">php100</a>
    <a href="#" class="test">phpchina</a>
    <a href="#" class="test">blueidea</a>
      

  3.   


    因为页面时从上到下加载的,所以你的ajax根本就没有获取到隐藏域中的值,要将隐藏域放到ajax之前显示,如下:
    <?php
        if($_POST["tem"]){
            $tem        =$_POST["tem"];
            $ip        =$_POST["ip"];
            //链接数据库
            $server        =mysql_connect("localhost","root","123");
            $connection    =mysql_select_db("laji",$server);        $sql="select * from record where ip=$ip";
            $result=mysql_query($sql);
            $row=mysql_fetch_array($result);
            if(empty($row)){
                echo "empty";
                mysql_query("insert into record (ip,tem) values ($ip,$tem)");
            }else{
                echo "not empty";
                mysql_query("update record set tem=$tem where ip=$ip");
            }
        }
        $ip=$_SERVER["REMOTE_ADDR"];
    ?>
    <input type="hidden" name="ip" id="ip" value="<?php echo $ip ?>" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        function record(){
            $(".test").click(function(e){
                $.ajax({
                    type:"POST",
                    url:"laji.php",
                    data:{
                        tem:$(this).html(),
                        ip:$("#ip").val()
                    }
                });
            });
        }
        $(function(){record();});
    </script><a href="#" class="test">csdn</a>
    <a href="#" class="test">php100</a>
    <a href="#" class="test">phpchina</a>
    <a href="#" class="test">blueidea</a>
      

  4.   

    一样的 $(function(){record();});
    这句就指定了整页加载完后并且 click事件发生才会post的
      

  5.   

    mysql_query("update record set tem='$tem' where ip='$ip'");
      

  6.   

    mysql_query("update record set tem='$tem' where ip='$ip'");谢谢哥,原来这样,但有个疑问,以前学过好像变量是不允许放在''单引号里面的,单引号里面的都是字符串,为什么这里可以呢?
      

  7.   

    谢谢,最后一个问题,你一楼的回复为什么做那么多的判断?
    这节is_server不行吗?
      

  8.   

    有的时候你那样获取获取不到,就像有A、B、C,有的时候A有,有的时候B有,也有的时候都有