Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQL
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // sql to create table
- $sql = "CREATE TABLE MyGuests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )";
- if ($conn->query($sql) === TRUE) {
- echo "Table MyGuests created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- $conn->close();
- ?>
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) {
- echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
- }
- } else {
- echo "0 results";
- }
- $conn->close();
- ?>
- SESSIONS
- <?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>
- login.php
- <html>
- <head>
- <title>Login Form</title>
- </head>
- <body>
- <h2>Login Form</h2>
- <form method="post" action="checklogin.php">
- User Id: <input type="text" name="uid"><br>
- Password: <input type="password" name="pw"><br>
- <input type="submit" value="Login">
- </form>
- </body>
- </html>
- checklogin.php
- <?php
- $uid = $_POST['uid'];
- $pw = $_POST['pw'];
- if($uid == 'arun' and $pw == 'arun123')
- {
- session_start();
- $_SESSION['sid']=session_id();
- header("location:securepage.php");
- }
- ?>
- securepage.php
- <?php
- session_start();
- if($_SESSION['sid']==session_id())
- {
- echo "Welcome to you<br>";
- echo "<a href='logout.php'>Logout</a>";
- }
- else
- {
- header("location:login.php");
- }
- ?>
- logout.php
- <?php
- echo "Logged out scuccessfully";
- session_start();
- session_destroy();
- setcookie(PHPSESSID,session_id(),time()-1);
- ?>
- PHP EMAIL VALIDATION
- <?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"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- }
- }
- 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"]);
- }
- }
- ?>
- PHP VALIDATION
- <?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"]);
- }
- }
- ?>
- <html>
- <head>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $name = test_input($_POST["name"]);
- $email = test_input($_POST["email"]);
- $website = test_input($_POST["website"]);
- $comment = test_input($_POST["comment"]);
- $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>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name">
- <br><br>
- E-mail: <input type="text" name="email">
- <br><br>
- Website: <input type="text" name="website">
- <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
- <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>
- 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.";
- }
- }
- ?>
- <?php
- $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
- // Output one line until end-of-file
- while(!feof($myfile)) {
- echo fgets($myfile) . "<br>";
- }
- fclose($myfile);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement