PHP Session et Cookies: Exemple d’utilisation de session_start()

 
<?php session_start(); ?>
<html>
 <head>
  <title>Session starter</title>
 <head>
 <body>
 <a href="next.php?<?php echo( SID ); ?>">Next page</a>
 <hr>
 PHPSESSID = 
 <?php 
  echo ( session_id() ); 
 ?>
 </body>
 
</html>
  
  

PHP: Fermer un fichier avec la fonction fclosde()

 
int fclose (int filepointer)
<?
    $file = "data.txt";
    if (file_exists($file)) :
         $fh = fopen($file, "r");
         fclose($fh);
    else:
         print "File $file does not exist!";
    endif;
?>
  
  

PHP: Vérifier si un élément est un répertoire

<html>
<head>
<title>Is the file a directory: is_dir()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_dir( $f )?"":"not ")."a directory<br>";
}
?>
</body>
</html>
           
       

PHP: Vérifier si un fichier existe sinon le créer

<?php
  $myfile = "./test.txt";
  if (file_exists ($myfile)){
    $msg="File already exists. ";
  }else{
    $myfile = @fopen ($myfile, "w+") or die ("Couldn't create the file"  );
    $msg= "File created! " ;
    fclose ($myfile);
  }
  echo "$msg"; 
?>
           
       

PHP Session et Cookies: Vérifier si les cookies sont activés

 
<?php
    if(!isset($_GET['testcookie'])) {
        setcookie("testcookie", "test value");
        header("Location:  {$_SERVER["PHP_SELF"]}?testcookie=1");
        exit;
    } else {
        if(isset($_COOKIE['testcookie'])) {
            setcookie("testcookie");
            echo "You have cookies enabled";
        } else {
            echo "You do not support cookies!";
        }
    }
?>
  
  

PHP Session et Cookies: Supprimer les variables de session

 
<?php
   session_start();
   $_SESSION['username'] = "jason";
   echo "Your username is: ".$_SESSION['username'].".<br />";
   unset($_SESSION['username']);
   echo "Username now set to: ".$_SESSION['username'].".";
?>
  
  

PHP Session et Cookies: Supprimer un Cookie

 
<?php
setcookie ( "cookie_user", "test", time () + 60 * 60 * 24 * 30 );
setcookie ( "cookie_pass", md5 ( "test" ), time () + 60 * 60 * 24 * 30 );
function logout() {
  setcookie ( "cookie_user", "", time () + 60 * 60 * 24 * 30 );
  setcookie ( "cookie_pass", "", time () + 60 * 60 * 24 * 30 );
}
logout ();
echo $_COOKIE ['cookie_user'] . "<br />";
echo "You have successfully logged out.";
?>
  
  

PHP: Supprimer un fichier

<?php
   $file_delete = "home/meeta/my.php";
   
   if (unlink($file_delete)) {
      echo "The file was deleted successfully.", " ";
   } else {
      echo "The specified file could not be deleted. Please try again.", " ";
   }
?>
           
       

PHP: Vérifier si Le fichier est accessible en écriture

<html>
<head>
<title>Is the file writable: is_writable()</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f is ".(is_writable( $f )?"":"not ")."writable<br>";
}
?>
</body>
</html>
           
       

PHP: Trouver la date de modification d’un fichier

<html>
<head>
<title>File changed time</title>
</head>
<body>
<?php
$file = "test.txt";
outputFileTestInfo( $file );
function outputFileTestInfo( $f ){
   if ( ! file_exists( $f ) ){
       print "$f does not exist<BR>";
      return;
   }
   print "$f was changed on ".date( "D d M Y g:i A", filectime( $f ) )."<br>";
}
?>
</body>
</html>