Teman

Pencarian

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 cookies
print_r($_COOKIE);
?>

Program18-3.php menggunakan fungsi isset() untuk mencari apakah cookie telah dibuat.
<html>
<body>
<?php
if (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>

PHP File Handling

Dalam PHP, fungsi fopen() digunakan untuk membuka file.

Membuka File

 

Program16-1.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>

Mode pembukaan file
Mode
Keterangan
r
Read only. Starts at the beginning of the file
r+
Read/Write. Starts at the beginning of the file
w
Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+
Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a
Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+
Read/Append. Preserves file content by writing to the end of the file
x
Write only. Creates a new file. Returns FALSE and an error if file already exists
x+
Read/Write. Creates a new file. Returns FALSE and an error if file already exists
Catatan: Jika fopen() tidak dapat membuka file, maka akan mengembalikan nilai 0 (false).

Program16-2.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");

?>
</body>
</html>


Menutup File

Program16-3.php
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>


Memeriksa EOF (End Of File)

 

Catatan: Kita tidak dapat membaca file yang terbuka dalam mode w, a, dan x!
if (feof($file)) echo "End of file";

Membaca file baris per baris (fgets())

 

Program16-4.php
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>


Membaca file karakter per karakter (fgetc())

 

Program16-5.php

<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file)) 
  { 
  echo fgetc($file); 
  }
fclose($file);
?>