Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file creation or opening
- <?php
- $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
- $txt = "John Doe\n";
- fwrite($myfile, $txt);
- $txt = "Jane Doe\n";
- fwrite($myfile, $txt);
- fclose($myfile);
- ?>
- <?php
- $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
- echo fread($myfile,filesize("webdictionary.txt"));
- fclose($myfile);
- ?>
- uploading file
- <!DOCTYPE html>
- <html>
- <body>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- </body>
- </html>
- <?php
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- } else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- }
- ?>
- Cookies
- <?php
- $cookie_name = "user";
- $cookie_value = "John Doe";
- setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
- ?>
- <html>
- <body>
- <?php
- if(!isset($_COOKIE[$cookie_name])) {
- echo "Cookie named '" . $cookie_name . "' is not set!";
- } else {
- echo "Cookie '" . $cookie_name . "' is set!<br>";
- echo "Value is: " . $_COOKIE[$cookie_name];
- }
- ?>
- </body>
- </html
- Modify a cookie
- <?php
- $cookie_name = "user";
- $cookie_value = "Alex Porter";
- setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
- ?>
- <html>
- <body>
- <?php
- if(!isset($_COOKIE[$cookie_name])) {
- echo "Cookie named '" . $cookie_name . "' is not set!";
- } else {
- echo "Cookie '" . $cookie_name . "' is set!<br>";
- echo "Value is: " . $_COOKIE[$cookie_name];
- }
- ?>
- </body>
- </html>
- Delete a cookie
- <?php
- // set the expiration date to one hour ago
- setcookie("user", "", time() - 3600);
- ?>
- <html>
- <body>
- <?php
- echo "Cookie 'user' is deleted.";
- ?>
- </body>
- </html>
- Check if enabled
- <?php
- setcookie("test_cookie", "test", time() + 3600, '/');
- ?>
- <html>
- <body>
- <?php
- if(count($_COOKIE) > 0) {
- echo "Cookies are enabled.";
- } else {
- echo "Cookies are disabled.";
- }
- ?>
- </body>
- </html>
- sessions
- <?php
- // Start the session
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- // Set session variables
- $_SESSION["favcolor"] = "green";
- $_SESSION["favanimal"] = "cat";
- echo "Session variables are set.";
- ?>
- </body>
- </html>
- ------------------------------------------------------------------------
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- // Echo session variables that were set on previous page
- echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
- echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
- ?>
- </body>
- </html>
- -----------------------------------------------------------------------
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- // to change a session variable, just overwrite it
- $_SESSION["favcolor"] = "yellow";
- print_r($_SESSION);
- ?>
- </body>
- </html>
- ----------------------------------------------------------------------
- <?php
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <?php
- // remove all session variables
- session_unset();
- // destroy the session
- session_destroy();
- ?>
- </body>
- </html>
- -----------------------------------------------------------------------------
- MAIL
- <form method="post" action="mail.php" name="contact"><br />
- Your name :<input type="text" name="username" /><br />
- Email :<input type="text" name="useremail" /><br />
- Message :<textarea cols="5" rows="5" name="message"></textarea><br />
- <input type="submit" value="SEND EMAIL" name="submit" /><br />
- </form>
- <?php
- $from="kssyam@gmail.com";
- $to="to@gmail.com";
- $subject="Test Mail";
- $message="Hi ,<br />".
- "This is my test email for learning how to send email using PHP mail function <br />".
- "Thank You";
- $headers = "From: $fromrnContent-type: text/html";
- if(mail($to,$subject, $message, $headers))
- {
- echo "MAIL SENT SUCCESSFULLY";
- }
- else
- {
- echo "SOME ISSUE WITH MAIL SENDING in SERVER";
- }
- ?>
- -----------------------------------------------------------------------------------
- form validation php
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"></textarea>
- <br><br>
- Gender:
- <input type="radio" name="gender" value="female">Female
- <input type="radio" name="gender" value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- </html>
- ---------------------------------------------------------------------
- MySQL
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Check connection
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- $result = mysqli_query($conn, $sql);
- if (mysqli_num_rows($result) > 0) {
- // output data of each row
- while($row = mysqli_fetch_assoc($result)) {
- echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
- }
- } else {
- echo "0 results";
- }
- mysqli_close($conn);
- ?>
Add Comment
Please, Sign In to add comment