Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

PHP - Amankan halaman khusus anggota dengan sistem login

Saya sarankan Anda mengubah pendekatan Anda.

Meskipun sekilas file-file contoh ini terlihat seperti banyak, setelah Anda mempelajarinya, Anda akan melihat bahwa itu benar-benar jauh lebih sederhana dan pendekatan logis daripada arah yang Anda tuju sekarang.

Pertama, pindahkan db connect / login stuff ke file terpisah, dan require atau include file itu di bagian atas setiap halaman PHP:

INIT.PHP

    // Create connection
    $conn = new mysqli($servername, $username, $password, $db);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 

    //Might as well also load your functions page here, so they are always available
    require_once('fn/functions.php');
?>

Sekarang, lihat bagaimana kami menggunakannya di halaman Indeks (dan Dibatasi)?

INDEX.PHP

<?php
    require_once('inc/head.inc.php');
    require_once('fn/init.php');
?>

<body>
    <!-- Examples need jQuery, so load that... -->
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <!-- and our own file we will create next... -->
    <script type="text/javascript" src="js/index.js"></script>

    <div id="pageWrap">
        <div id="loginDIV">
            LoginID: <input type="text" id="liID" /><br>
            LoginPW: <input type="password" id="liPW" /><br>
            <input type="button" id="myButt" value="Login" />
        </div>
    </div>

JS/INDEX.JS

$(function(){
    $('#myButt').click(function(){
        var id = $('#liID').val();
        var pw = $('#liPW').val();
        $.ajax({
            type: 'post',
             url: 'ajax/login.php',
            data: 'id=' +id+ '&pw=' +pw,
            success: function(d){
                if (d.length) alert(d);
                if (d==1) {
                    window.location.href = 'restricted_page.php';
                }else{
                    $('#liID').val('');
                    $('#liPW').val('');
                    alert('Please try logging in again');
                }
        }
        });
    });//END myButt.click

}); //END document.ready

AJAX/LOGIN.PHP

<?php
    $id = $_POST['id'];
    $pw = $_POST['pw'];

    //Verify from database that ID and PW are okay
    //Note that you also should sanitize the data received from user

    if ( id and password authenticate ){
        //Use database lookups ot get this data: $un = `username` 

        //Use PHP sessions to set global variable values
        $_SESSION['username'] = $un;
        echo 1;
    }else{
        echo 'FAIL';
    }

RESTRICTED_PAGE.PHP

<?php
    if (!isset($_SESSION['username']) ){
        header('Location: ' .'index.php');
    }

    require_once('inc/head.inc.php');
    require_once('fn/init.php');
?>
<body>
    <h1>Welcome to the Admin Page, <?php echo $_SESSION['username']; ?>
    <!--  AND here go all teh restricted things you need a login to do. -->

Selengkapnya tentang AJAX - pelajari contoh sederhananya




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Kesalahan MySQL #1071 - Kunci yang ditentukan terlalu panjang; panjang kunci maksimal adalah 767 byte

  2. 4 Cara Mendaftar Semua Tampilan di MySQL

  3. Tes Koneksi PDO

  4. Cara Mendapatkan Catatan dari 7 Hari Terakhir di MySQL

  5. 3 Cara Menemukan Baris yang Mengandung Huruf Besar di MySQL