我正在尝试做一个登录类,检查所有字段是否正确,如果是,那么就是proccess。
我的代码:(login.php
)
<?php
require('sql.php');
class login {
private $user;
private $email;
private $doc;
private $password;
function login($field, $pass){
$user = $field;
$email = $field;
$doc = strtoupper($field);
$password = $pass;
$this->getUser($user, $password, $r) ? $r : $this->getEmail($email, $password, $r) ? $r : $this->getDoc($doc, $password, $r) ? $r : null;
}
private function getUser($u, $p, &$r){
global $sql;
$count = 0;
$check = $sql->query("SELECT ... ");
while($row = $check->fetch_object()){
$count++;
$r = $row;
}
$count == 1 ? true : false;
}
private function getEmail($e, $p, &$r){ same as getUser()... }
private function getDoc($d, $p, &$r){ same as getUser()... }
}
?>
现在在Index(index.php)
<html>
ALL HTML STUFF WITH THE FORM
</html>
<?php
require('login.php');
if(isset($_POST['submit'])){
$login = new login(trim($sql->real_escape_string($_POST['user'])), md5(trim($sql->real_escape_string($_POST['pass']))));
if($login != null){
echo "SUCCESSFUL: ".$login->user;
}else{
echo "INCORRECT PASSWORD";
}
}
?>
我们的想法是获得$login
之类的值$login->user
。但是告诉我一个错误......
我怎样才能做到这一点?我的错误在哪里?
答案 0 :(得分:1)
这会因私人$ user而导致错误;
公开$ user;因为私人会员不允许从外面访问
或者您可以执行以下操作
public function getUsername(){
return $this->username;
}
&#13;
并通过echo $ Obj-&gt; getusername();
访问它答案 1 :(得分:0)
我不是说所有这些都有效。只是想知道我的意思。
您想要的是获取用户信息。
为此,您创建了一个班级login
。
为什么不创建一个User
类来检索和存储信息。
如果构造函数出错。只需返回null
。
class User {
private $user;
private $email;
private $doc;
private $password;
private $detail1;
private $detail2;
private $detail3;
private $detail4;
public function __constructfunction login($field, $pass){
$user = $field;
$email = $field;
$doc = strtoupper($field);
$password = $pass;
$error = false;
//Detail for User
$count = 0;
$check = $sql->query("SELECT ... WHERE username='.$this->user.' AND pass = '.$this->password.'");
while($row = $check->fetch_object()){
$count++;
$r = $row;
}
if($count == 1)
{
$this->detail1 = $row['detail1'];
$this->detail2 = $row['detail2'];
}
else
{
$error = true;
}
//Detail for Doc
$count = 0;
$check = $sql->query("SELECT ... WHERE username='.$this->user.' AND pass = '.$this->password.'");
while($row = $check->fetch_object()){
$count++;
$r = $row;
}
if($count == 1)
{
$this->detail3 = $row['detail3'];
$this->detail4 = $row['detail4'];
}
else
{
$error = true;
}
if(true === $error)
return null;
}
public function getUser(){
return $this->user;
}
public function getEmail(){
return $this->doc;
}
public function getDoc(){
return $this->doc;
}
//Maybe not usefull
public function getPassword(){
return $this->password;
}
}
答案 2 :(得分:0)
好的,谢谢大家,但我解决了问题!!
在登录的构造函数中我放了一个值。
public function login($field, $pass, &$r)
然后在index.php
$login = new login(//user, //pass, $r);
echo "SUCCESSFUL: ".$r->user;
这返回SQL查询的值。这就是我想要的。
再次感谢。