Example of using COOKIES in php (login)
Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. Through PHP, you can create and retrieve cookie values
Mainly set cookies when logging in and out. To save login and SecurityExit
1: Set
//设置cookies的值 _setcookies($_rows['tg_username'], $_rows['tg_uniqid'],$_clean['time']); _location(null,'index.php');
on the login page Step 2: Generate cookie,
/**
* _setcookies
* @param unknown $_username
* @param unknown $_uniqid
*/
function _setcookies($_username,$_uniqid,$_time){
setcookie('username',$_username);
setcookie('uniqid',$_uniqid);
switch ($_time) {
case '0' : // 设置浏览器进程
setcookie ( 'username', $_username );
setcookie ( 'uniqid', $_uniqid );
break;
case '1' : // 一天的进程
setcookie ( 'username', $_username, $_time () + 86400 );
setcookie ( 'uniqid', $_uniqid, $_time () + 86400 );
break;
case '2' : // 一周的进程
setcookie ( 'username', $_username, $_time () + 604800 );
setcookie ( 'uniqid', $_uniqid, $_time () + 604800 );
break;
case '0' : // 一个月的进程
setcookie ( 'username', $_username, $_time () + 2592000 );
setcookie ( 'uniqid', $_uniqid, $_time () + 2592000 );
break;
}
}Step 3: Log in Set
//登录的情况,就是防止登陆后通过在浏览器中直接连接再次登录
function _login_state(){
if (!isset($_COOKIE['username'])) {
_alert_back("登录状态无法就行本操作!");
}
}
//删除cookies
function _unsetcookies(){
setcookie('username','',time()-1);
setcookie('uniqid','',time()-1);
_session_destroy();//删除session
_location(null,'index.php'); //做跳转
}in the status. The fourth step is to add
//登录状态 _login_state();
to the login page and exit page. Two methods for displaying data: configure in the data configuration file , my It is mysql.func.php
/**
* _fetch_array 只能获取一条数据组
* @param $_sql
*/
function _fetch_array($_sql) {
return mysql_fetch_array(_query($_sql),MYSQL_ASSOC);
}
/**
* _fetch_array_list可以返回指定数据集的所有数据
* @param unknown $_result
* @return multitype:
*/
function _fetch_array_list($_result){
return mysql_fetch_array($_result,MYSQL_ASSOC);
}Finally assigned value in the page
//从数据库里提取数据获取结果集
//我们必须每次重新读取结果集,而不是从新去执行SQL语句。
$_result = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC");
<?php while (!!$_rows = _fetch_array_list($_result)) {?>
<dl>
<dd class="user"><?php echo $_rows['tg_username']?><?php echo $_rows['tg_sex']?></dd>
<dt><img src="<?php echo $_rows['tg_face']?>" alt="" /></dt>The above is the detailed content of Example of using COOKIES in php (login). For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
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)
How to read a CSV file in PHP?
Aug 29, 2025 am 08:06 AM
ToreadaCSVfileinPHP,usefopen()toopenthefile,fgetcsv()inalooptoreadeachrowasanarray,andfclose()tocloseit;handleheaderswithaseparatefgetcsv()callandspecifydelimitersasneeded,ensuringproperfilepathsandUTF-8encodingforspecialcharacters.
How to use AJAX with php
Aug 29, 2025 am 08:58 AM
AJAXwithPHPenablesdynamicwebappsbysendingasynchronousrequestswithoutpagereloads.1.CreateHTMLwithJavaScriptusingfetch()tosenddata.2.BuildaPHPscripttoprocessPOSTdataandreturnresponses.3.UseJSONforcomplexdatahandling.4.Alwayssanitizeinputsanddebugviabro
Edit bookmarks in chrome
Aug 27, 2025 am 12:03 AM
Chrome bookmark editing is simple and practical. Users can enter the bookmark manager through the shortcut keys Ctrl Shift O (Windows) or Cmd Shift O (Mac), or enter through the browser menu; 1. When editing a single bookmark, right-click to select "Edit", modify the title or URL and click "Finish" to save; 2. When organizing bookmarks in batches, you can hold Ctrl (or Cmd) to multiple-choice bookmarks in the bookmark manager, right-click to select "Move to" or "Copy to" the target folder; 3. When exporting and importing bookmarks, click the "Solve" button to select "Export Bookmark" to save as HTML file, and then restore it through the "Import Bookmark" function if necessary.
What is the difference between isset and empty in php
Aug 27, 2025 am 08:38 AM
isset()checksifavariableexistsandisnotnull,returningtrueevenforzero,false,oremptystringvalues;2.empty()checksifavariableisnull,false,0,"0","",orundefined,returningtrueforthese"falsy"values;3.isset()returnsfalsefornon-exi
How to get the current date and time in PHP?
Aug 31, 2025 am 01:36 AM
Usedate('Y-m-dH:i:s')withdate_default_timezone_set()togetcurrentdateandtimeinPHP,ensuringaccurateresultsbysettingthedesiredtimezonelike'America/New_York'beforecallingdate().
How to configure SMTP for sending mail in php
Aug 27, 2025 am 08:08 AM
Answer: Using the PHPMailer library to configure the SMTP server can enable sending mails through SMTP in PHP applications. PHPMailer needs to be installed, set up SMTP host, port, encryption method and authentication credentials of Gmail, write code to set sender, recipient, topic and content, enable 2FA and use application password to ensure that the server allows SMTP connection, and finally call the send method to send email.
How to create an object in php
Aug 27, 2025 am 08:45 AM
To create a PHP object, you need to define the class first, and then instantiate it with the new keyword. For example, after defining the Car class and setting properties and constructing methods, create an object through $myCar=newCar("red","Toyota"), and then use -> to access its properties and methods, such as $myCar->color and $myCar->showInfo(). Each object has independent data and can create multiple instances.
How to set an error reporting level in PHP?
Aug 31, 2025 am 06:48 AM
Useerror_reporting()toseterrorlevelsinPHP,suchasE_ALLfordevelopmentor0forproduction,andcontroldisplayorloggingviaini_set()toenhancedebuggingandsecurity.


