正如标题所说我有setcookie()函数的问题 - 它根本不起作用。如果你能指出我犯了什么错误,我真的很感激。我正在尝试创建一个简单的登录脚本:
的index.php:
<?php
include_once("mysql.php");
include_once("security.php");
global $connection;
if (isset($_POST['name']) and isset($_POST['pass'])) {
if($Action -> login($_POST['name'],$_POST['pass']) == true){
$Security -> create_cookie($_POST['name']);
echo "Login successful!";
}
elseif($Action -> login($_POST['name'],$_POST['pass']) == false){
echo "Login failed! Given information was incorrect.";
}
}
else{
include_once("layout/login.php");
}
?>
mysql.php:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "game";
global $connection;
$connection = new mysqli($host,$user,$pass,$db_name);
if($connection->connect_errno) echo "Connection to the database failed:". $connection->connect_errno.".";?>
布局/ login.php中:
<?php
echo "<form name =\"login\" method = \"post\" action=\"\" style=\"border: 1px;\">
Name:</br>
<input type=\"text\" name=\"name\" length=\"28\"><br/>
Password:</br>
<input type=\"password\" name=\"pass\"></br></br>
<input type=\"submit\" name=\"button\" value=\"Login\"/>
</form>
";
?>
和security.php:
<?php
class Security{
function create_cookie($user){
global $connection;
//cookie settings
$name = "login_script";
$value = md5(md5(md5(time()).md5("65ds4fg6s1df56g165sdf"))."5sdfg5462gs1df6g8518s");
$time = time()+3600*24*14;
$path = "/";
$domain = "localhost";
$sec = 0;
$connection -> query("UPDATE users SET session = '$value' WHERE user = '$user' LIMIT 1");
return setcookie($name,$value,$time,$path,$domain,$sec);
}
function encrypt($phrase){
$salt = "d6f5g4e2rnwber21";
$extra_salt = "2n1k";
$pass = md5(md5($phrase).md5($salt)).$extra_salt;
return $pass;
}
}
class Action extends Security{
function login($urn, $pass){
global $connection;
$query = mysqli_query($connection,"SELECT password FROM users WHERE user = '$urn' LIMIT 1");
$result = mysqli_fetch_row($query);
if($result[0] == $this->encrypt($pass)){
return true;
}
elseif($result[0] != $this->encrypt($pass)){
return false;
}
else{
$error = mysqli_error();
$errno = mysqli_errno();
$report = "Unknown error occured: no. ".$errno.". Error: ".$error.".";
return $report;
}
}
$Security = new Security();
$Action = new Action();
?>
使用LiveHTTPHeader插件我得到了:
GET / HTTP/1.1
Host: localhost
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: lt,en-US;q=0.8,en;q=0.6,ru;q=0.4,pl;q=0.2
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 17
Content-Type: text/html
Date: Sat, 08 Aug 2015 08:03:23 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.9 (Win32) PHP/5.5.12
Set-Cookie: login_script=37fd88cddf9a75f8b758ba1a62317215; expires=Sat, 22-Aug-2015 08:03:23 GMT; Max-Age=1209600; path=/; domain=localhost
X-Powered-By: PHP/5.5.12
好的,问题解决了。原来Chrome不允许在localhost上设置cookie(127.0.0.1似乎也不起作用)。但是,如果您将domain参数保留为空,则chrome会自动将当前网站(即使它是localhost)作为Cookie的域。我希望这会为某人节省大量时间。我要感谢那些帮助过我的人。