php – 类的对象..无法转换为字符串

前端之家收集整理的这篇文章主要介绍了php – 类的对象..无法转换为字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我完成了我的第一堂课,但我无法将对象转换回字符串.
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'),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(),"/");
        setcookie("kkuserid",$validated->encrypt(),"/");//disguised cookie name
    }
    session_start();
    $_SESSION['authenticated'] = $row[0];
    echo '1'; //true
    exit;
}
else
{
    echo '0'; //false
    exit;
}
?>
看看这一行:
$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'");
原文链接:https://www.f2er.com/php/137902.html

猜你在找的PHP相关文章