我制作了我的第一堂课,但我无法将对象转换回字符串。
class Cryption
{
var $data;
var $salt;
function __construct($data, $salt)
{
$this->data = $data;
$this->salt = $salt;
}
function sha512()
{
$sodium = 'Na';
return hash_hmac("sha512", $this->data . $this->salt, $sodium);
}
function encrypt()
{
$salt = substr(sha512(($this->key), 'brownies'), 0, 30);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $this->data, MCRYPT_MODE_CBC, md5($salt)));
}
当我使用它时:
$password = new Cryption(mysql_real_escape_string(trim($_POST['password'])), 'pepper');
$password->sha512();
它说'PHP Catchable致命错误:类Cryption的对象无法转换为字符串'
我真的不知道怎么把它变成一个字符串。愿有人帮帮我吗?
谢谢。
编辑:
<?php
require("config.php");
include("includes/cryption/cryption.php");
$username = mysql_real_escape_string(trim($_POST['username']));
$password = new Cryption(mysql_real_escape_string(trim($_POST['password'])), 'pepper'); //use a different salt next time such as a special salt for each user
$password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$password'");
$row = mysql_fetch_row($result);
$count = mysql_num_rows($result);
if ($count == 1) {
if (isset($_POST['remember'])) {
session_start();
$_SESSION['user'] = array(
'id' => $row[0],
'username' => $row[1],
'password' => $row[2]
);
$userid = new Cryption($_SESSION['user']['id'], 'kkfishing');
$session = new Cryption($_SESSION['user']['username'], 'kkfishing');
$validated = new Cryption($_SESSION['user']['password'], 'kkfishing');
setcookie("uniqueid", $userid->encrypt(), time() + 60 * 60 * 24 * 100, "/"); //100 days
setcookie("kksessionid", $session->encrypt(), time() + 60 * 60 * 24 * 100, "/");
setcookie("kkuserid", $validated->encrypt(), time() + 60 * 60 * 24 * 100, "/");//disguised cookie name
}
session_start();
$_SESSION['authenticated'] = $row[0];
echo '1'; //true
exit;
}
else
{
echo '0'; //false
exit;
}
?>
答案 0 :(得分:14)
看看这一行:
$password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$password'");
$password
是一个对象。它应该是:
$pw = $password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$pw'");
答案 1 :(得分:0)
以如下错误方式初始化构造函数变量时会遇到同样的错误:
function __construct($data, $salt)
{
$this->$data = $data; //mistakenly using $ (dollar) sign after `$this->`
$this->$salt = $salt;
}
正确的初始化方式是:
function __construct($data, $salt)
{
$this->data = $data;
$this->salt = $salt;
}