Guest User

PHP

a guest
Apr 23rd, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. **To store current date,time in a COOKIE**
  2. <?php
  3. $intwomonths = 60*60*24*60 + time();
  4. setcookie('lastvisit', date("G:i- m/d/y"),$intwomonths);
  5. if(isset($_COOKIE['lastvisit']))
  6.  
  7. {
  8. $visit = $_COOKIE[lastvisit];
  9. echo "your last visit was-".$visit;
  10. }
  11. else
  12. echo "you have got some stale cookies!";
  13. ?>
  14.  
  15. **To store page view counts in SESSION**
  16. <?php
  17. session_start();
  18.  
  19. if(isset($_SESSION['count']))
  20. {
  21. echo "Your session count: ".$_SESSION['count']."<br />";
  22. $_SESSION['count']++;
  23. }
  24. else
  25. {
  26. $_SESSION['count'] = 1;
  27. echo "Session does not exist";
  28. }
  29.  
  30. ?>
  31.  
  32. **XHTML form**
  33. <form>
  34. <fieldset>
  35. <legend>Log In</legend>
  36. <label>Username: <input type="text"></label>
  37. <label>Password: <input type="password"></label>
  38. <input type="submit" value="Submit">
  39. </fieldset>
  40. </form>
  41.  
  42. **READ A FILE**
  43. <html>
  44.  
  45. <head>
  46. <title>Reading a file using PHP</title>
  47. </head>
  48.  
  49. <body>
  50.  
  51. <?php
  52. $filename = "tmp.txt";
  53. $file = fopen( $filename, "r" );
  54.  
  55. if( $file == false ) {
  56. echo ( "Error in opening file" );
  57. exit();
  58. }
  59.  
  60. $filesize = filesize( $filename );
  61. $filetext = fread( $file, $filesize );
  62. fclose( $file );
  63.  
  64. echo ( "File size : $filesize bytes" );
  65. echo ( "<pre>$filetext</pre>" );
  66. ?>
  67.  
  68. </body>
  69. </html>
  70.  
  71. **WRITE A FILE**
  72. <?php
  73. $filename = "/home/user/guest/newfile.txt";
  74. $file = fopen( $filename, "w" );
  75.  
  76. if( $file == false ) {
  77. echo ( "Error in opening new file" );
  78. exit();
  79. }
  80. fwrite( $file, "This is a simple test\n" );
  81. fclose( $file );
  82. ?>
  83. <html>
  84.  
  85. <head>
  86. <title>Writing a file using PHP</title>
  87. </head>
  88.  
  89. <body>
  90.  
  91. <?php
  92. $filename = "newfile.txt";
  93. $file = fopen( $filename, "r" );
  94.  
  95. if( $file == false ) {
  96. echo ( "Error in opening file" );
  97. exit();
  98. }
  99.  
  100. $filesize = filesize( $filename );
  101. $filetext = fread( $file, $filesize );
  102.  
  103. fclose( $file );
  104.  
  105. echo ( "File size : $filesize bytes" );
  106. echo ( "$filetext" );
  107. echo("file name: $filename");
  108. ?>
  109.  
  110. </body>
  111. </html>
Add Comment
Please, Sign In to add comment