PHPは、簡単なユーザー登録、ログイン、および個人情報変更インターフェースを実装します

1.データベースに接続します

データベースの名前はeatingで、userテーブルはユーザー情報を格納するために使用されます

//connect_mysql.php 
<?php 
header( "Content-type:text / html; charset = utf-8"); //エンコーディングとページ
ヘッダーを定義します( "Access-Control-Allow-Origin:*"); //クロスドメインの問題

session_start(); //セッションを開く

$ host = 'localhost'; //ホストアドレス
$ database = 'eating'; //データベース名
$ username = 'root'; //データベースユーザー名
$ password = ' 123456 '; //データベースパスワード
/ *
 データベースに接続
 * / 
$ link = mysqli_connect($ host、$ username、$ password);     
mysqli_select_db($ link、 "eating"); 
mysqli_query($ link、 "set names'utf8' "); //エンコーディング変換
if(!$ link){ 

    die("データベースに接続できませんでした。\ n "。Mysqli_error($ link)); //接続エラーを診断します
} 
?>12345678910111213141516171819202122

2.ユーザーログイン

  phpはユーザーログインを実装し、ユーザー名は電話番号であり、phpバックグラウンドはhttp postメソッドを介してフロントエンドから送信されたjsonデータを受け入れて解析し、ユーザーが存在するかどうかを判断し、存在
しない場合は-1を返します。ユーザーが存在する場合は、ユーザー名とパスワードが一貫しているかどうかを確認し、一貫している場合は1を返し、パスワードが間違っている場合は-2を返します。

//login.php 
<?php 
header( "Content-type:text / html; charset = utf-8"); 
header( "Access-Control-Allow-Origin:*"); // Cross-domain 

session_start() ; 

if(isset($ _ POST ["button"])&& $ _POST ["button"] == "Login"){ 
    $ tel = $ _POST ["tel"]; //ユーザーの電話番号
    $ password = $ _POST [ "password"]; //ユーザーパスワード

    // include( "connect_mysql.php"); 
    require_once( 'connect_mysql.php'); 

    //ユーザーが存在するかどうかを判別
    $ sql = "SELECT * FROM user WHERE user_tel = '$ _POST [tel] '; "; 

    $ result = mysqli_query($ link、$ sql); // sqlステートメントを実行し、クエリの後に結果セットを返します
    $ rows = mysqli_num_rows($ result);//結果セットの行数を返し

    ますif($ rows == 0){//ユーザーが存在しません
        $ json_arr = array( 'success' => -1); 
    }
    else { 
        $ sql = "SELECT password FROM user WHERE password = '$ _POST [password]' AND user_tel = '$ _POST [tel]'"; 
        $ result = mysqli_query($ link、$ sql); 
        $ rows = mysqli_num_rows($ result); 

        if($ rows == 1){//密码正确
            $ json_arr = array( 'success' => 1); 

            $ _SESSION ["is_login"] = "true"; 
            $ _SESSION ["tel"] = $ tel; 
            $ _SESSION ["password"] = $ password; 
        } 
        else {//密码错误
            $ json_arr = array( 'success' => -2); 
        } 
    } 
    $ login_json = json_encode($ json_arr、TRUE); //変換化修正jsonデータ
    エコー$ login_json;
} 123456789101112131415161718192021222324252627282930313233343536373839404142

3.ユーザー登録

  ユーザーは携帯電話番号を使用してアカウントを登録します。phpバックグラウンドは、最初にユーザーが送信した情報が完全かどうかを確認し、不完全な場合は0を返します。次に、
ユーザーの携帯電話番号が登録されているかどうかを確認します。0携帯電話番号が登録されている場合はフロントエンド-1に戻ります。携帯電話番号が登録されていない場合は
ユーザーが2回入力したパスワードが同じかどうかを確認します。同じ場合は登録に成功します。 、および1がフロントエンドに返され、そうでない場合は-2が返され、そうでない場合は-3が返されます。

// register.php 
<?php 

header( "Access-Control-Allow-Origin:*"); 

session_start(); 

if(isset($ _ POST ["submit"])&& $ _POST ["submit"] == "马上注册"){ 
    $ tel = $ _POST ["tel"]; 
    $ password = $ _POST ["password"]; 
    $ password_conf = $ _POST ["confirm"]; 
    $ hometown = $ _POST ["hometown"]; 
    $ tasty = $ _POST ["tasty"]; 
    $ type_of_cooking = $ _POST ["type_of_cooking"]; 

    if($ tel == "" || $ password == "" || $ password == "" || $ password_conf == "" || $ hometown == "" || $ tasty == "" || $ type_of_cooking == ""

            include "connect_mysql.php"; 
            //require_once('connect_mysql.php '); 

            $ sql = "select tel from user where tel =' $ _POST [tel] '"; // SQLステートメント
            $ result = mysqli_query($ link、 $ sql); // SQLステートメントを実行します
            $ num = mysqli_num_rows($ result); //実行結果の影響を受ける行数をカウントします
            if($ num){//ユーザーがすでに存在する場合

                $ json_arr = array( 'success '=> -1); 
            } 
            else {//現在登録されているユーザー名は存在しません

                $ sql_insert = "insert into user(user_tel、password、hometown、tasty、type_of_cooking)
                      values(' $ _ POST [tel] '、' $ _POST [password] '、' $ _POST [hometown] '、' $ _POST [tasty] '、'$ _POST [type_of_cooking] ') ";
                $ res_insert = mysqli_query($ link、$ sql_insert); 
                // $ num_insert = mysql_num_rows($ res_insert); 
                if($ res_insert){//正常に登録されました
                    $ json_arr = array( 'success' => 1); 
                } 
                else {/ /システムがビジーです。後でもう一度やり直してください
                    $ json_arr = array( 'success' => -3); 
                } 
            } 
        } 
        else {//パスワードに一貫性がありません
            $ json_arr = array( 'success' => -2); 
        } 
    } 
    $ register_json = json_encode($ json_arr、TRUE); 
    echo $ register_json; 
}
?> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253

4.ユーザー情報を変更します

//alter_info.php 
<?php 

header( "Content-type:text / html; charset = utf-8"); 
header( "Access-Control-Allow-Origin:*"); 

session_start(); 

if(isset($ _ POST ["submit"])&& $ _POST ["submit"] == "确认修改"){ 
    include "connect_mysql.php"; 
    // require_once( 'connect_mysql.php'); 

    $ tel = $ _POST ['tel']; 
    $ new_hometown = $ _POST ['new_hometown']; //家乡
    $ new_tasty = $ _POST ['new_tasty']; //口味
    $ new_type_of_cooking = $ _POST ['new_type_of_cooking']; //商品クラス(干/汤)

    $ sql = "UPDATE user SET hometown = '$ new_hometown'、tasty = '$ new_tasty'、type_of_cooking = '$ new_type_of_cooking' 

    if($ result){//情報を正常に変更する
        $ json_arr = array( 'success' => 1); 
    } 
    else {//情報を正常に変更できなかった
        $ json_arr = array( 'success' => 0); 
    } 

    $ login_json = json_encode($ json_arr); 
    echo $ login_json; 
} 1234567891011121314151617181920212223242526272829303132

5.パスワードを変更します

//alter_password.php 
<?php 

header( "Content-type:text / html; charset = utf-8"); 
header( "Access-Control-Allow-Origin:*"); 

session_start(); 

if(isset($ _ POST ["submit"])&& $ _POST ["submit"] == "修改密码"){ 
    // include "connect_mysql.php"; 
    require_once( 'connect_mysql.php'); 

    $ tel = $ _POST ['tel']; 
    $ old_password = $ _POST ['old_password']; 

    $ sql = "SELECT password、user_tel FROM user WHERE password = '$ old_password' AND user_tel = '$ tel';"; 
    $ result = mysqli_query($ link、$ sql); 
    $ row = mysqli_num_rows($ result); 

    if($ row == 1){ 
        $ new_password = $ _POST ['new_password']; 
        $ new_password_conf = $ _POST ['new_password_conf'];

        if($ new_password == $ new_password_conf){ 
            $ sql = "UPDATE user SET password = '$ new_password' WHERE user_tel = '$ tel';"; 
            $ result = mysqli_query($ link、$ sql); 

            if($ result) {//パスワードの変更に成功しました
                $ arr = array( 'success' => 1); 
            } 
        } 
    } 
    else {//パスワードの変更に失敗しました
        $ arr ['success'] = 0; 
    } 
    $ json_arr = json_encode($ arr、TRUE) ; 
    echo $ json_arr; 
}

おすすめ

転載: blog.csdn.net/an17822307871/article/details/112915216