Home > Backend Development > PHP Tutorial > Cookie protection against forgery security explanation_PHP tutorial

Cookie protection against forgery security explanation_PHP tutorial

WBOY
Release: 2016-07-13 17:11:42
Original
781 people have browsed it

In the past, when we first wrote PHP, we needed administrator authentication to do the backend. This is usually done using COOKIE, especially for PHP enthusiasts who are new to PHP:
admin/login.php

if(Username&&Password is correct) {
setcookie('admin',1, time()+36400
);
echo
'Login successful'
;
}


if($_COOKIE[admin] == 1) {
echo
'Has permission'
;
}


However, this will cause great security risks. Many browsers can directly modify COOKIE, or modify it directly in the system.

As long as you forge cookies, you will have administrative rights

To be safe, do this:

if(Username&&Password is correct) {
setcookie('userid',The user’s ID in the system,time()+36400
);
setcookie('userpass',The user’s 32-bit md5 password in the system,time()+36400
);
echo
'Login successful'
;
}


Do this when judging permissions:

if($_COOKIE[userid]) {
$query = mysql_query(select * user table where userid = '$_COOKIE[userid]' and userpass = '$_COOKIE[userpass]'
);
$row = mysql_fetch_array($query
) ;
if(
$row[rank] <> 1
) {
echo
'No permission'
;
}
}


In this way, forging cookies will have no effect

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629572.htmlTechArticleIn the past, when we first wrote PHP, we needed administrator authentication to do the backend. This is usually done using COOKIE, especially for PHP enthusiasts who are new to PHP: admin/login.php if(username and password are correct...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template