以下是完整代码的一部分:
<?php
class Token
{ public static function generate()
{ return Session::put('token'), md5(uniqid()));
}
public static function check($token)
{ $tokenName = 'token';
if(Session::exists($tokenName) && $token ===
Session::get($tokenName))
{ Session::delete($tokenName);
return true;
}
return false;
}
}
///////////////////////////////////////////////////////////////
class Session
{
public static function exists($name)
{ return (isset($_SESSION[$name])) ? true : false;
}
public static function put($name, $value)
{ return $_SESSION[$name] = $value;
}
public static function get($name)
{ return $_SESSION[$name];
}
public static function delete($name)
{ if(self::exists($name))
{ unset($_SESSION[$name]);
}
}
///////////////////////////////////////////////////////////////
class Input
{
public static function get($item)
{ if(isset($_POST[$item]))
{ return $_POST[$item];
}
return '';
}
}
//////////////////////////////////////////////////////////////
if(isset($_POST['username']) && isset($_POST['password']))
{ if(Token::check(Input::get('token')))
{ $validate = new Validate();
$validation = ................;
if($validation->passed())
{ $user = new User();
$login = $user->login(Input::get('username'),
Input::get('password'));
if($login)
{ echo 'Success';
Redirect::to('index.php');
}
else
{ echo 'Sorry, login failed!';
}
} //validation passed
else
{ foreach($validation->errors() as $error)
{ echo $error, '<br>';
}
echo "<script> setTimeout(\"location.href =
'index.php';\",30000); </script>";
}
}
}
?>
<form action="" method="POST">
<P>
<label for="username">Username</label>
<input type="text" name="username" id="username"
autocomplete="off">
</P>
<P>
<label for="password">Password</label>
<input type="password" name="password" id="password"
autocomplete="off">
</P>
<P>
<input type="hidden" name="token" value="<?php echo
Token::generate(); ?>">
<input type="submit" value="LOG IN">
</P>
</form>
当我这样做时:
public static function check($token)
{ $tokenName = Config::get('session/token_name');
$testing = Session::get($tokenName);
echo join(' ', array_map(function($c) { return sprintf('%02x', $c); }, array_map('ord', str_split($token))));
echo "<br>";
echo join(' ', array_map(function($c) { return sprintf('%02x', $c); }, array_map('ord', str_split($testing))));
if(Session::exists($tokenName) && $token === Session::get($tokenName))
{ Session::delete($tokenName);
return true;
}
return false;
}
}
我明白了:
ef bb bf ef bb bf ef bb bf 39 31 64 32 61 66 63 31 63 61 38 63 39 32 39 66 62 63 63 35 35 61 36 38 37 31 65 36 37 33 65 61
and:
39 31 64 32 61 66 63 31 63 61 38 63 39 32 39 66 62 63 63 35 35 61 36 38 37 31 65 36 37 33 65 61
不满足条件:
$token === Session::get($tokenName)
我用过:
<form action="" method="POST" accept-charset="utf-8">
但这不起作用。我所知道的是它与UTF-8编码的字节顺序标记有关,但我不知道如何删除额外的字节。关于如何解决这个问题的任何建议?我使用的是PHP 5.5.12版