简体   繁体   中英

PHP MYSQL ajax Yii framework login error

Hi im trying to establish a php/mysql login in Yii framework. I have currently build the relevent scripts it does not make the call to the webservice script, can anybody sought it out for me, below are the relevent functions of the three scripts according to calls repectively.

Login.php

$(function() {
        $("#login-form").submit(function( event ) {

            event.preventDefault();

            var username = $("#username").val();
            var password = $("#password").val();
            var params = {username: username, password: password};

            var sendingData = { id: 1, jsonrpc: "2.0", method: "login", params:params};
            $.ajax({
                type: "POST",
                url: "/webservice.php", 
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                async: false,
                data: JSON.stringify(sendingData),
                success: function( msg ) {
                  alert( "Data Saved: " + msg.result );
                  if(msg.result != false){
                  window.location = "index.php";
                  }else
                  {
                      alert("Invalid Username or Password");
                      window.location = "login.php";
                  }

                }
              });

       });
  });

Webservice.php

    function login($params) {
    $main = new main();

    $ret = $main->login($params["username"], $params["password"]);
    if($ret != false){
        $_SESSION['usertype'] = $ret->usertype;
        $_SESSION['username'] = $params["username"];
        }
    return $ret;}

***main.php***

    'db'=>array(
            'class'=>'CDbConnection',
            'connectionString'=>'mysql:host=localhost;dbname=login',
            'username'=>'root',
            'password'=>'root',
            'emulatePrepare'=>true,  // needed by some MySQL installations
        ),

    function login($usr,$pwd) {
        $connection=Yii::app()->db;
        $query = "SELECT * FROM login";
        $dataReader=$command->query();
        $row = mysql_fetch_array($dataReader);
        $log = new stdClass();
        if($row) {
            $pro->accountID = (int)$row['accountID'];
            $pro->accountname = $row['accountname'];
            $pro->usertype = (int)$row['usertype'];
                    $string = rand() . 'SURVAYLAND' . rand() . $usr. $pwd;
                $_SESSION['SURVEY_AUTHENTICATE_KEY'] = md5($string);
        } else {
            $pro = false;
        }


}

You are going about it all wrong. The code you wrote is not the Yii way.

Because Yii is an application framework , it forces you to apply a certain coding structure that will fit into its frame. Your code does not. To apply this, go to the Yii documentation site and review how the framework works.

Yii also generates a ready to use application that contains user login, so you already have something to work on.

However, I can tell you the the start of your problems is your call to the remote URL. "/webservice.php" should be a call to "/index.php" with parameters of a route and action. for example.

        url: "/index.php?r=webservice/login", 

This url should point to a Yii controller file WebserviceController.php containing a method actionLogin.

class WebserviceController extends Controller
{
  public function actionLogin()
  {
     so_domething_here();
  }
}

References : http://www.yiiframework.com/doc/guide/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM