PHP Cookies
cookie biasanya digunakan untuk mengidentifikasi user.
Membuat Cookie (setcookie())
Catatan: fungsi setcookie() harus ditulis sebelmu tag <html>.
setcookie(name, value, expire, path, domain); |
Program18-1.php
<?php setcookie("user", "Alex Porter", time()+3600);?><html><body></body></html> |
Mengambil nilai Cookie ($_COOKIE)
Program18-2.php
<?php
// Print a cookie
echo $_COOKIE["user"];// A way to view all cookiesprint_r($_COOKIE);?> |
Program18-3.php menggunakan fungsi isset() untuk mencari apakah cookie telah dibuat.
<html><body><?phpif (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />";else echo "Welcome guest!<br />";?></body></html> |
Menghapus Cookie
Program18-4.php
<?php // set the expiration date to one hour ago
setcookie("user", "", time()-3600);?> |
Bagaimana jika browser yang kita gunakan tidak mendukung Cookie
Gunakan pasangan form input untuk mengirim data dan form retrieve untuk mengambil data seperti contoh di bawah ini.
Program18-5.php
<html><body><form action="welcome.php" method="post">Name: <input type="text" name="name" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html> |
welcome.php.
<html><body>Welcome <?php echo $_POST["name"]; ?>.<br />You are <?php echo $_POST["age"]; ?> years old.</body></html> |



0 komentar:
Posting Komentar