Sesi PHP Hancurkan setelah 30 Menit

0
(0)

oleh Vincy. Terakhir diubah pada 6 September 2022.

PHP memiliki fungsi inti session_destroy() untuk menghapus semua nilai sesi. Ini adalah fungsi tanpa argumen sederhana yang mengembalikan nilai boolean benar atau salah.

ID sesi PHP disimpan dalam cookie secara default. Umumnya file cookie sesi itu bernama PHPSESSID. Fungsi session_destroy tidak akan menghapus id sesi dalam cookie.

Untuk menghancurkan sesi ‘sepenuhnya’, ID sesi juga harus tidak disetel.

Contoh cepat ini menggunakan session_destroy() untuk menghancurkan sesi. Ini menggunakan metode set_cookie() untuk mematikan keseluruhan dengan kedaluwarsa ID sesi PHP.

Contoh cepat

menghancurkan-session.php

<?php
// Always remember to initialize the session,
// even before attempting to destroy it.

// Destroy all the session variables.
$_SESSION = array();

// delete the session cookie also to destroy the session
if (ini_get("session.use_cookies")) {
    $cookieParam = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000, $cookieParam["path"], $cookieParam["domain"], $cookieParam["secure"], $cookieParam["httponly"]);
}

// as a last step, destroy the session.
session_destroy();

Catatan:

  1. Gunakan session_start() untuk memulai kembali sesi setelah penghancuran sesi PHP.
  2. Gunakan PHP $_SESSION untuk menghapus variabel sesi tertentu. Untuk versi PHP yang lebih lama, gunakan session_unset().

sesi php menghancurkan output

Tentang contoh session_destroy() login ini

Mari kita buat kode contoh login untuk menggunakan sesi PHP, session_destroy dan semuanya. Ini memungkinkan pengguna untuk masuk dan keluar dari sesi saat ini. Gunakan kode ini jika Anda mencari pendaftaran pengguna lengkap dan login dalam skrip PHP.

Contoh ini menyediakan fitur kedaluwarsa sesi login otomatis.

Halaman arahan dengan formulir login

Formulir ini memposting nama pengguna dan kata sandi yang dimasukkan oleh pengguna. Ini memverifikasi kredensial login di PHP.

Pada login yang berhasil, ia menyimpan status login ke dalam sesi PHP. Ini menetapkan waktu kedaluwarsa menjadi 30 menit dari waktu login terakhir.

Ini menyimpan waktu login terakhir dan waktu kedaluwarsa ke dalam sesi PHP. Kedua variabel sesi ini digunakan untuk mengakhiri sesi secara otomatis.

login.php

<?php
session_start();
$expirtyMinutes = 1;
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel="stylesheet" href="https://phppot.com/php/php-session-destroy/style.css" type="text/css" />
<link rel="stylesheet" href="form.css" type="text/css" />
</head>
<body>
    <div class="phppot-container">
        <h1>Login</h1>
        <form name="login-form" method="post">
            <table>
                <tr>
                    <td>Username</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Sign in"
                        name="submit"></td>
                </tr>
            </table>
        </form>
<?php
if (isset($_POST['submit'])) {
    $usernameRef = "admin";
    $passwordRef = "test";
    $username = $_POST['username'];
    $password = $_POST['password'];

    // here in this example code focus is session destroy / expiry only
    // refer for registration and login code https://phppot.com/php/user-registration-in-php-with-login-form-with-mysql-and-code-download/
    if ($usernameRef == $username && $passwordRef == $password) {
        $_SESSION['login-user'] = $username;
        // login time is stored as reference
        $_SESSION['ref-time'] = time();
        // Storing the logged in time.
        // Expiring session in 30 minutes from the login time.
        // See this is 30 minutes from login time. It is not 'last active time'.
        // If you want to expire after last active time, then this time needs
        // to be updated after every use of the system.
        // you can adjust $expirtyMinutes as per your need
        // for testing this code, change it to 1, so that the session
        // will expire in one minute
        // set the expiry time and
        $_SESSION['expiry-time'] = time() + ($expirtyMinutes * 60);
        // redirect to home
        // do not include home page, it should be a redirect
        header('Location: home.php');
    } else {
        echo "Wrong username or password. Try again!";
    }
}
?>
</div>
</body>
</html>

Gabung

Dashboard memvalidasi sesi login PHP dan menampilkan link login, dan logout

Ini adalah halaman target yang dialihkan setelah login. Ini menunjukkan tautan keluar jika sesi masuk ada.

Setelah waktu habis, ia akan memanggil kode destroy-session.php untuk menghancurkan semua sesi.

Jika waktu kedaluwarsa 30 menit tercapai atau sesi kosong, ia meminta pengguna untuk masuk.

home.php

<?php
session_start();
?>
<html>
<head>
<title>PHP Session Destroy after 30 Minutes</title>
<link rel="stylesheet" href="https://phppot.com/php/php-session-destroy/style.css" type="text/css" />
<link rel="stylesheet" href="form.css" type="text/css" />
</head>
<body>
    <div class="phppot-container">
<?php
if (! isset($_SESSION['login-user'])) {
    echo "Login again!<br><br>";
    echo "<a href="login.php">Login</a>";
} else {
    $currentTime = time();
    if ($currentTime > $_SESSION['expiry-time']) {
        require_once __DIR__ . '/destroy-session.php';
        echo "Session expired!<br><br><a href="login.php">Login</a>";
    } else {
        ?>
        <h1>Welcome <?php echo $_SESSION['login-user'];?>!</h1>
        <a href="logout.php">Log out</a>
<?php
    }
}
?>
</div>
</body>
</html>

Kode PHP ini digunakan untuk pengguna yang ingin logout sebelum waktu berakhirnya sesi.

Itu menghancurkan sesi dengan meminta kode destroy-session.php. Kemudian, itu mengarahkan pengguna ke halaman login.

logout.php

<?php
session_start();
require_once __DIR__ . '/destroy-session.php';
header('Location: login.php');
?>

Saya harap contoh ini membantu untuk memahami cara menghancurkan sesi PHP. Dan juga, ini adalah skenario sempurna yang cocok untuk menjelaskan perlunya menghancurkan sesi.
Unduh

Kembali ke Atas


Source link

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.