php 类的对象 .. 无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7156440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object of class .. could not be converted to string
提问by Kyle
I made my first class and I'm having trouble converting the objects back into strings.
我上了第一堂课,但在将对象转换回字符串时遇到了麻烦。
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)));
}
When I use it:
当我使用它时:
$password = new Cryption(mysql_real_escape_string(trim($_POST['password'])), 'pepper');
$password->sha512();
It says 'PHP Catchable fatal error: Object of class Cryption could not be converted to string'
它说“PHP Catchable 致命错误:无法将类 Cryption 的对象转换为字符串”
I don't really know how to get it back into a string. May someone please help me?
我真的不知道如何将其恢复为字符串。有人可以帮助我吗?
Thank you.
谢谢你。
Edit:
编辑:
<?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;
}
?>
回答by Andreas
Look at this lines:
看看这几行:
$password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$password'");
$password
is an object. It should be:
$password
是一个对象。它应该是:
$pw = $password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$pw'");