如何使用mysql发送不同页面的人

时间:2014-03-20 23:22:40

标签: php html mysql

我有一个登录页面,有两种帐户。我添加了一个列,确切地知道它们是“代理”还是“代理”。我想要做的是当连接“代理”帐户时,他将转到account1.php,而不是“notagent”帐户将转到account2.php。我怎样才能做到这一点?这是我的代码到目前为止的样子......

<?php 

include_once('connectiondb.php');

class User {

    private $db;

    public function __construct() {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }

    public function Login($name, $pass){
        if(!empty($name) && !empty($pass)) {
            $st = $this->db->prepare("select * from users where username=? and password=?");
            $st->bindParam(1,$name);
            $st->bindParam(2,$pass);
            $st->execute();

            if($st->rowCount() == 1) {
                header("Location: compte.php");
                die();
            } else {
                echo 'Incorrect username or password.';
            }
        } else {
            echo 'Please enter username and password';
        }
    }

我认为不是标题(...)我应该放另一个if语句,但我应该在其中写什么?如何验证他是代理人还是非代理人帐户?

3 个答案:

答案 0 :(得分:1)

您可以获取结果并在rowCount()支票

中执行另一个if语句
public function Login($name, $pass){
    if(!empty($name) && !empty($pass)) {
        $st = $this->db->prepare("select * from users where username=? and password=?");
        $st->bindParam(1,$name);
        $st->bindParam(2,$pass);
        $st->execute();
        $results = $st->fetchObject();

        if($st->rowCount() == 1) {
            if ($results->colName == "agent") { // Change colName to any column that contains the agent and notagent value
                header("Location: account1.php");
            } else {
                header("Location: account2.php");
            }
        } else {
            echo 'Incorrect username or password.';
        }
    } else {
        echo 'Please enter username and password';
    }
}

答案 1 :(得分:1)

简单!你走了!

<?php

public function Login($name, $pass){
    // Check in the begining instead of using nested ifs
    if (empty($name) || empty($pass))
        exit('Please enter username and password');

    // Fetch from db
    $st = $this->db->prepare("select * from users where username=? and password=?");
    $st->bindParam(1,$name);
    $st->bindParam(2,$pass);
    $st->execute();

    // Check if no match
    if ($st->rowCount() != 1)
        exit('Incorrect username or password.');

    // Fetch first (and only) match
    $row = $st->fetch_object()

    // Check the agent field
    $nr = $row->agent=="agent"?"1":"2";

    // Redirect to that page
    exit(header("Location: /account$nr.php"));
}

?>

答案 2 :(得分:0)

在这里你http://www.php.net/manual/de/function.header.php

只需使用

header('Location: http://www.example.com/thefileyouwant.php');

但是:只有当脚本运行部分运行时才会产生回声。