letscheat1234

Web tech php

May 4th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1.  
  2. file creation or opening
  3.  
  4. <?php
  5. $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  6. $txt = "John Doe\n";
  7. fwrite($myfile, $txt);
  8. $txt = "Jane Doe\n";
  9. fwrite($myfile, $txt);
  10. fclose($myfile);
  11. ?>
  12.  
  13. <?php
  14. $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
  15. echo fread($myfile,filesize("webdictionary.txt"));
  16. fclose($myfile);
  17. ?>
  18.  
  19.  
  20.  
  21.  
  22.  
  23. uploading file
  24.  
  25.  
  26. <!DOCTYPE html>
  27. <html>
  28. <body>
  29.  
  30. <form action="upload.php" method="post" enctype="multipart/form-data">
  31. Select image to upload:
  32. <input type="file" name="fileToUpload" id="fileToUpload">
  33. <input type="submit" value="Upload Image" name="submit">
  34. </form>
  35.  
  36. </body>
  37. </html>
  38.  
  39.  
  40. <?php
  41. $target_dir = "uploads/";
  42. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  43. $uploadOk = 1;
  44. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  45. // Check if image file is a actual image or fake image
  46. if(isset($_POST["submit"])) {
  47. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  48. if($check !== false) {
  49. echo "File is an image - " . $check["mime"] . ".";
  50. $uploadOk = 1;
  51. } else {
  52. echo "File is not an image.";
  53. $uploadOk = 0;
  54. }
  55. }
  56. // Check if file already exists
  57. if (file_exists($target_file)) {
  58. echo "Sorry, file already exists.";
  59. $uploadOk = 0;
  60. }
  61. // Check file size
  62. if ($_FILES["fileToUpload"]["size"] > 500000) {
  63. echo "Sorry, your file is too large.";
  64. $uploadOk = 0;
  65. }
  66. // Allow certain file formats
  67. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  68. && $imageFileType != "gif" ) {
  69. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  70. $uploadOk = 0;
  71. }
  72. // Check if $uploadOk is set to 0 by an error
  73. if ($uploadOk == 0) {
  74. echo "Sorry, your file was not uploaded.";
  75. // if everything is ok, try to upload file
  76. } else {
  77. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  78. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  79. } else {
  80. echo "Sorry, there was an error uploading your file.";
  81. }
  82. }
  83. ?>
  84.  
  85.  
  86.  
  87. Cookies
  88.  
  89. <?php
  90. $cookie_name = "user";
  91. $cookie_value = "John Doe";
  92. setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
  93. ?>
  94. <html>
  95. <body>
  96.  
  97. <?php
  98. if(!isset($_COOKIE[$cookie_name])) {
  99. echo "Cookie named '" . $cookie_name . "' is not set!";
  100. } else {
  101. echo "Cookie '" . $cookie_name . "' is set!<br>";
  102. echo "Value is: " . $_COOKIE[$cookie_name];
  103. }
  104. ?>
  105.  
  106. </body>
  107. </html
  108.  
  109. Modify a cookie
  110.  
  111. <?php
  112. $cookie_name = "user";
  113. $cookie_value = "Alex Porter";
  114. setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
  115. ?>
  116. <html>
  117. <body>
  118.  
  119. <?php
  120. if(!isset($_COOKIE[$cookie_name])) {
  121. echo "Cookie named '" . $cookie_name . "' is not set!";
  122. } else {
  123. echo "Cookie '" . $cookie_name . "' is set!<br>";
  124. echo "Value is: " . $_COOKIE[$cookie_name];
  125. }
  126. ?>
  127.  
  128. </body>
  129. </html>
  130.  
  131.  
  132. Delete a cookie
  133.  
  134. <?php
  135. // set the expiration date to one hour ago
  136. setcookie("user", "", time() - 3600);
  137. ?>
  138. <html>
  139. <body>
  140.  
  141. <?php
  142. echo "Cookie 'user' is deleted.";
  143. ?>
  144.  
  145. </body>
  146. </html>
  147.  
  148.  
  149. Check if enabled
  150.  
  151. <?php
  152. setcookie("test_cookie", "test", time() + 3600, '/');
  153. ?>
  154. <html>
  155. <body>
  156.  
  157. <?php
  158. if(count($_COOKIE) > 0) {
  159. echo "Cookies are enabled.";
  160. } else {
  161. echo "Cookies are disabled.";
  162. }
  163. ?>
  164.  
  165. </body>
  166. </html>
  167.  
  168.  
  169. sessions
  170.  
  171.  
  172. <?php
  173. // Start the session
  174. session_start();
  175. ?>
  176. <!DOCTYPE html>
  177. <html>
  178. <body>
  179.  
  180. <?php
  181. // Set session variables
  182. $_SESSION["favcolor"] = "green";
  183. $_SESSION["favanimal"] = "cat";
  184. echo "Session variables are set.";
  185. ?>
  186.  
  187. </body>
  188. </html>
  189. ------------------------------------------------------------------------
  190. <?php
  191. session_start();
  192. ?>
  193. <!DOCTYPE html>
  194. <html>
  195. <body>
  196.  
  197. <?php
  198. // Echo session variables that were set on previous page
  199. echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
  200. echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
  201. ?>
  202.  
  203. </body>
  204. </html>
  205.  
  206.  
  207. -----------------------------------------------------------------------
  208.  
  209. <?php
  210. session_start();
  211. ?>
  212. <!DOCTYPE html>
  213. <html>
  214. <body>
  215.  
  216. <?php
  217. // to change a session variable, just overwrite it
  218. $_SESSION["favcolor"] = "yellow";
  219. print_r($_SESSION);
  220. ?>
  221.  
  222. </body>
  223. </html>
  224.  
  225. ----------------------------------------------------------------------
  226.  
  227.  
  228. <?php
  229. session_start();
  230. ?>
  231. <!DOCTYPE html>
  232. <html>
  233. <body>
  234.  
  235. <?php
  236. // remove all session variables
  237. session_unset();
  238.  
  239. // destroy the session
  240. session_destroy();
  241. ?>
  242.  
  243. </body>
  244. </html>
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. -----------------------------------------------------------------------------
  255. MAIL
  256.  
  257.  
  258. <form method="post" action="mail.php" name="contact"><br />
  259. Your name :<input type="text" name="username" /><br />
  260. Email :<input type="text" name="useremail" /><br />
  261. Message :<textarea cols="5" rows="5" name="message"></textarea><br />
  262. <input type="submit" value="SEND EMAIL" name="submit" /><br />
  263. </form>
  264.  
  265.  
  266.  
  267. <?php
  268. $from="kssyam@gmail.com";
  269. $to="to@gmail.com";
  270. $subject="Test Mail";
  271. $message="Hi ,<br />".
  272. "This is my test email for learning how to send email using PHP mail function <br />".
  273. "Thank You";
  274. $headers = "From: $fromrnContent-type: text/html";
  275. if(mail($to,$subject, $message, $headers))
  276. {
  277. echo "MAIL SENT SUCCESSFULLY";
  278. }
  279. else
  280. {
  281. echo "SOME ISSUE WITH MAIL SENDING in SERVER";
  282. }
  283. ?>
  284.  
  285.  
  286.  
  287.  
  288.  
  289. -----------------------------------------------------------------------------------
  290.  
  291. form validation php
  292.  
  293.  
  294. <!DOCTYPE HTML>
  295. <html>
  296. <head>
  297. <style>
  298. .error {color: #FF0000;}
  299. </style>
  300. </head>
  301. <body>
  302.  
  303. <?php
  304. // define variables and set to empty values
  305. $nameErr = $emailErr = $genderErr = $websiteErr = "";
  306. $name = $email = $gender = $comment = $website = "";
  307.  
  308. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  309. if (empty($_POST["name"])) {
  310. $nameErr = "Name is required";
  311. } else {
  312. $name = test_input($_POST["name"]);
  313. }
  314.  
  315. if (empty($_POST["email"])) {
  316. $emailErr = "Email is required";
  317. } else {
  318. $email = test_input($_POST["email"]);
  319. }
  320.  
  321. if (empty($_POST["website"])) {
  322. $website = "";
  323. } else {
  324. $website = test_input($_POST["website"]);
  325. }
  326.  
  327. if (empty($_POST["comment"])) {
  328. $comment = "";
  329. } else {
  330. $comment = test_input($_POST["comment"]);
  331. }
  332.  
  333. if (empty($_POST["gender"])) {
  334. $genderErr = "Gender is required";
  335. } else {
  336. $gender = test_input($_POST["gender"]);
  337. }
  338. }
  339.  
  340. function test_input($data) {
  341. $data = trim($data);
  342. $data = stripslashes($data);
  343. $data = htmlspecialchars($data);
  344. return $data;
  345. }
  346. ?>
  347.  
  348. <h2>PHP Form Validation Example</h2>
  349. <p><span class="error">* required field.</span></p>
  350. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  351. Name: <input type="text" name="name">
  352. <span class="error">* <?php echo $nameErr;?></span>
  353. <br><br>
  354. E-mail: <input type="text" name="email">
  355. <span class="error">* <?php echo $emailErr;?></span>
  356. <br><br>
  357. Website: <input type="text" name="website">
  358. <span class="error"><?php echo $websiteErr;?></span>
  359. <br><br>
  360. Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  361. <br><br>
  362. Gender:
  363. <input type="radio" name="gender" value="female">Female
  364. <input type="radio" name="gender" value="male">Male
  365. <span class="error">* <?php echo $genderErr;?></span>
  366. <br><br>
  367. <input type="submit" name="submit" value="Submit">
  368. </form>
  369.  
  370. <?php
  371. echo "<h2>Your Input:</h2>";
  372. echo $name;
  373. echo "<br>";
  374. echo $email;
  375. echo "<br>";
  376. echo $website;
  377. echo "<br>";
  378. echo $comment;
  379. echo "<br>";
  380. echo $gender;
  381. ?>
  382.  
  383. </body>
  384. </html>
  385.  
  386. ---------------------------------------------------------------------
  387.  
  388. MySQL
  389.  
  390. <?php
  391. $servername = "localhost";
  392. $username = "username";
  393. $password = "password";
  394. $dbname = "myDB";
  395.  
  396. // Create connection
  397. $conn = mysqli_connect($servername, $username, $password, $dbname);
  398. // Check connection
  399. if (!$conn) {
  400. die("Connection failed: " . mysqli_connect_error());
  401. }
  402.  
  403. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  404. $result = mysqli_query($conn, $sql);
  405.  
  406. if (mysqli_num_rows($result) > 0) {
  407. // output data of each row
  408. while($row = mysqli_fetch_assoc($result)) {
  409. echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  410. }
  411. } else {
  412. echo "0 results";
  413. }
  414.  
  415. mysqli_close($conn);
  416. ?>
Add Comment
Please, Sign In to add comment