PHP基于cookie与session统计网站访问量并输出显示的方法,cookiesession
PHP基于cookie与session统计网站访问量并输出显示的方法,cookiesession
本文实例讲述了PHP基于cookie与session统计网站访问量并输出显示的方法。分享给大家供大家参考,具体如下:
<?php $f_open = fopen("count.txt","r+"); //打开指定的文件 $count = fgets($f_open); //读取文件中的数据 if(empty($_COOKIE['cookie_name'])){ //判断COOKIE的是否存在 setcookie("cookie_name",value,time()+1800); //如果不存在,则创建COOKIE $count = $count + 1; //将变量$count的值加1 rewind($f_open); //打开指定的文件 fwrite($f_open,$count); //向文件中写入新的数据 fclose($f_open); //关闭文件 } ?>
<?php session_start(); include("conn/conn.php"); $data1=date("Y-m-d"); //获取当前访问时间 $data2=substr(date("Y-m-d"),0,7); $ip=getenv('REMOTE_ADDR'); if($_SESSION[temp]=="" || $_SESSION[temp]==NULL){ //判断$_SESSION[temp]==""的值是否为空,其中的temp为自定义的变量 //使用数据库存储数据 $select=mysql_query("select * from tb_count10 where data1='$data1' and ip='$ip'"); if(mysql_num_rows($select)>0){ $query1="update tb_count10 set counts=counts+1 where data1='$data1' and ip='$ip'"; $result1=mysql_query($query1); }else{ $query="insert into tb_count10(counts,data1,data2,ip)values('1','$data1','$data2','$ip')"; $result=mysql_query($query); } $_SESSION[temp]=1; //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1 } ?>
<?php //以图形的形式输出数据库中的记录数 $query="select sum(counts) as counts from tb_count04 ";//查询数据库中总的访问量 $result=mysql_query($query); $visitor=mysql_result($result,0,'counts'); echo "----------"; echo "<strong>网站的访问量: </strong>"; //以图形的方式显示访问次数 //对补位数字0的处理 $len=strlen($visitor); //获取字符串的长度 $str=str_repeat("0",6-$len); //获取6-$len个数字0 for($i=0;$i<strlen($str);$i++){ //获取变量$str的字符串长度 $result=$str[$i]; $result='<img src=images/0.gif alt="PHP基于cookie与session统计网站访问量并输出显示的方法,cookiesession" >'; echo $result; //循环输出$result的结果 } //对数据库中数据的处理 for($i=0;$i<strlen($visitor);$i++){ //获取字符串的长度 $result=$visitor[$i]; switch($result){ //如果值为"0",则输出0.gif图片 case "0"; $ret[$i]="0.gif";break; case "1"; $ret[$i]="1.gif";break; case "2"; $ret[$i]="2.gif";break; case "3"; $ret[$i]="3.gif";break; case "4"; $ret[$i]="4.gif";break; case "5"; $ret[$i]="5.gif";break; case "6"; $ret[$i]="6.gif";break; case "7"; $ret[$i]="7.gif";break; case "8"; $ret[$i]="8.gif";break; case "9"; $ret[$i]="9.gif";break; } echo "<img src=images/".$ret[$i].". alt="PHP基于cookie与session统计网站访问量并输出显示的方法,cookiesession" >"; //输出访问次数 } ?>
更多关于PHP操作cookie与session相关内容感兴趣的读者可查看本站专题:《PHP中cookie用法总结》及《PHP中session问题总结》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- PHP中Session和Cookie是如何操作的
- php中session与cookie的比较
- thinkphp中session和cookie无效的解决方法
- PHP会话控制:Session与Cookie详解
- PHP中cookie和session的区别实例分析
- ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
- php设置session值和cookies的学习示例
- 深入理解PHP中的Session和Cookie
- PHP5中Cookie与 Session使用详解
- php session和cookie使用说明

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
