dongqiao5573 2016-03-04 21:07
浏览 33

从AJAX调用创建PHP对象的实例

I'm playing with using AJAX to perform a simple login function using PHP. I haven't been able to get the AJAX call to successfully create an instance of the Controller class in the login handler file. I feel like I'm just not seeing something that is really basic.

I want to just say thank you in advance for any help! Also, this is not meant to be a real website or a real login script. I understand there are SO many security holes the way it is currently written. I fully intent to add all the bells, whistles, validation, etc. necessary to turn this simple code into something useful as soon as I understand the mistake.

The 4 files that should communicate with each other are as follows:

  1. view/login.php

    function signIn( )
    {
        if(checkString( ))
        {
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "username=" + $("#username").val( ) + "&password=" + $("#password").val( ),
                dataType: "html",
                async: false,
                cache: false,
                success: function(result){
                    $("#temp_container").html(result);
                }
            });
        }
    }
    
  2. AJAX.php

    <?php
    if(isset($_POST['username']) && !empty($_POST['password']))
    {
        $controller = new IndexController( );
        $result = $controller->login($_POST['username'], $_POST['password']);
    
        if($result > 0)
            $user_validation = array('true', 'view/chat_app.php');
        else
            $user_validation = array('false', 'index.php?error_num=1');
    
        echo json_encode($user_validation);
    }
    else if(isset($_POST['username']) && empty($_POST['password']))
    {
        //notify the user they didn't put in a password
    }
    ?>
    
  3. Controller.php

    <?php
    include_once("model/indexModel.php");
    
    class IndexController 
    {
        public $model;
    
        public function __construct( ) 
        {
            $model = new IndexModel( );
        }
    
        public function login($username, $password)
        {
            $result = $model->login($username, $password);
            if($result >= 1)
                return true;
            else
                return false;
        }
    }
    ?>
    
  4. Model.php

    <?php
    include_once("config/config.php");
    $db = new mysqli($GLOBALS['config']['db_host'], $GLOBALS['config']['db_username'], $GLOBALS['config']['db_password'], $GLOBALS['config']['db_name']);
    
    class IndexModel 
    {
        public function login($username, $password) 
        {
            global $db;
    
            $statement = $db->prepare("SELECT 1 FROM authorized_users 
                                       WHERE username = ?
                                             AND password = ?");
            $statement->bind_param('ss', $username, $password);
            $statement->execute( );
            $statement->close( );
    
            return $statement->affected_rows;
        }
    }
    ?>
    
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器