· 9 years ago · Apr 23, 2017, 05:10 PM
1SQL
2<?php
3$servername = "localhost";
4$username = "username";
5$password = "password";
6$dbname = "myDB";
7
8// Create connection
9$conn = new mysqli($servername, $username, $password, $dbname);
10// Check connection
11if ($conn->connect_error) {
12 die("Connection failed: " . $conn->connect_error);
13}
14
15// sql to create table
16$sql = "CREATE TABLE MyGuests (
17id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
18firstname VARCHAR(30) NOT NULL,
19lastname VARCHAR(30) NOT NULL,
20email VARCHAR(50),
21reg_date TIMESTAMP
22)";
23
24if ($conn->query($sql) === TRUE) {
25 echo "Table MyGuests created successfully";
26} else {
27 echo "Error creating table: " . $conn->error;
28}
29
30$conn->close();
31?>
32
33
34
35$sql = "SELECT id, firstname, lastname FROM MyGuests";
36$result = $conn->query($sql);
37
38if ($result->num_rows > 0) {
39 // output data of each row
40 while($row = $result->fetch_assoc()) {
41 echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
42 }
43} else {
44 echo "0 results";
45}
46$conn->close();
47?>
48
49
50SESSIONS
51<?php
52session_start();
53?>
54<!DOCTYPE html>
55<html>
56<body>
57
58<?php
59// Echo session variables that were set on previous page
60echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
61echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
62?>
63
64</body>
65</html>
66
67login.php
68
69<html>
70<head>
71<title>Login Form</title>
72</head>
73<body>
74<h2>Login Form</h2>
75<form method="post" action="checklogin.php">
76User Id: <input type="text" name="uid"><br>
77Password: <input type="password" name="pw"><br>
78<input type="submit" value="Login">
79</form>
80</body>
81</html>
82
83checklogin.php
84
85<?php
86$uid = $_POST['uid'];
87$pw = $_POST['pw'];
88
89if($uid == 'arun' and $pw == 'arun123')
90{
91 session_start();
92 $_SESSION['sid']=session_id();
93 header("location:securepage.php");
94}
95?>
96
97securepage.php
98
99<?php
100 session_start();
101 if($_SESSION['sid']==session_id())
102 {
103 echo "Welcome to you<br>";
104 echo "<a href='logout.php'>Logout</a>";
105 }
106 else
107 {
108 header("location:login.php");
109 }
110?>
111
112logout.php
113
114<?php
115
116 echo "Logged out scuccessfully";
117
118 session_start();
119 session_destroy();
120 setcookie(PHPSESSID,session_id(),time()-1);
121
122?>
123
124
125PHP EMAIL VALIDATION
126<?php
127// define variables and set to empty values
128$nameErr = $emailErr = $genderErr = $websiteErr = "";
129$name = $email = $gender = $comment = $website = "";
130
131if ($_SERVER["REQUEST_METHOD"] == "POST") {
132 if (empty($_POST["name"])) {
133 $nameErr = "Name is required";
134 } else {
135 $name = test_input($_POST["name"]);
136 // check if name only contains letters and whitespace
137 if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
138 $nameErr = "Only letters and white space allowed";
139 }
140 }
141
142 if (empty($_POST["email"])) {
143 $emailErr = "Email is required";
144 } else {
145 $email = test_input($_POST["email"]);
146 // check if e-mail address is well-formed
147 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
148 $emailErr = "Invalid email format";
149 }
150 }
151
152 if (empty($_POST["website"])) {
153 $website = "";
154 } else {
155 $website = test_input($_POST["website"]);
156 // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
157 if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
158 $websiteErr = "Invalid URL";
159 }
160 }
161
162 if (empty($_POST["comment"])) {
163 $comment = "";
164 } else {
165 $comment = test_input($_POST["comment"]);
166 }
167
168 if (empty($_POST["gender"])) {
169 $genderErr = "Gender is required";
170 } else {
171 $gender = test_input($_POST["gender"]);
172 }
173}
174?>
175
176
177
178PHP VALIDATION
179<?php
180// define variables and set to empty values
181$nameErr = $emailErr = $genderErr = $websiteErr = "";
182$name = $email = $gender = $comment = $website = "";
183
184if ($_SERVER["REQUEST_METHOD"] == "POST") {
185 if (empty($_POST["name"])) {
186 $nameErr = "Name is required";
187 } else {
188 $name = test_input($_POST["name"]);
189 }
190
191 if (empty($_POST["email"])) {
192 $emailErr = "Email is required";
193 } else {
194 $email = test_input($_POST["email"]);
195 }
196
197 if (empty($_POST["website"])) {
198 $website = "";
199 } else {
200 $website = test_input($_POST["website"]);
201 }
202
203 if (empty($_POST["comment"])) {
204 $comment = "";
205 } else {
206 $comment = test_input($_POST["comment"]);
207 }
208
209 if (empty($_POST["gender"])) {
210 $genderErr = "Gender is required";
211 } else {
212 $gender = test_input($_POST["gender"]);
213 }
214}
215?>
216
217
218<html>
219<head>
220</head>
221<body>
222
223<?php
224// define variables and set to empty values
225$name = $email = $gender = $comment = $website = "";
226
227if ($_SERVER["REQUEST_METHOD"] == "POST") {
228 $name = test_input($_POST["name"]);
229 $email = test_input($_POST["email"]);
230 $website = test_input($_POST["website"]);
231 $comment = test_input($_POST["comment"]);
232 $gender = test_input($_POST["gender"]);
233}
234
235function test_input($data) {
236 $data = trim($data);
237 $data = stripslashes($data);
238 $data = htmlspecialchars($data);
239 return $data;
240}
241?>
242
243<h2>PHP Form Validation Example</h2>
244<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
245 Name: <input type="text" name="name">
246 <br><br>
247 E-mail: <input type="text" name="email">
248 <br><br>
249 Website: <input type="text" name="website">
250 <br><br>
251 Comment: <textarea name="comment" rows="5" cols="40"></textarea>
252 <br><br>
253 Gender:
254 <input type="radio" name="gender" value="female">Female
255 <input type="radio" name="gender" value="male">Male
256 <br><br>
257 <input type="submit" name="submit" value="Submit">
258</form>
259
260<?php
261echo "<h2>Your Input:</h2>";
262echo $name;
263echo "<br>";
264echo $email;
265echo "<br>";
266echo $website;
267echo "<br>";
268echo $comment;
269echo "<br>";
270echo $gender;
271?>
272
273</body>
274</html>
275
276
277FILE
278
279<!DOCTYPE html>
280<html>
281<body>
282
283<form action="upload.php" method="post" enctype="multipart/form-data">
284 Select image to upload:
285 <input type="file" name="fileToUpload" id="fileToUpload">
286 <input type="submit" value="Upload Image" name="submit">
287</form>
288
289</body>
290</html>
291
292<?php
293$target_dir = "uploads/";
294$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
295$uploadOk = 1;
296$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
297// Check if image file is a actual image or fake image
298if(isset($_POST["submit"])) {
299 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
300 if($check !== false) {
301 echo "File is an image - " . $check["mime"] . ".";
302 $uploadOk = 1;
303 } else {
304 echo "File is not an image.";
305 $uploadOk = 0;
306 }
307}
308// Check if file already exists
309if (file_exists($target_file)) {
310 echo "Sorry, file already exists.";
311 $uploadOk = 0;
312}
313// Check file size
314if ($_FILES["fileToUpload"]["size"] > 500000) {
315 echo "Sorry, your file is too large.";
316 $uploadOk = 0;
317}
318// Allow certain file formats
319if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
320&& $imageFileType != "gif" ) {
321 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
322 $uploadOk = 0;
323}
324// Check if $uploadOk is set to 0 by an error
325if ($uploadOk == 0) {
326 echo "Sorry, your file was not uploaded.";
327// if everything is ok, try to upload file
328} else {
329 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
330 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
331 } else {
332 echo "Sorry, there was an error uploading your file.";
333 }
334}
335?>
336fread();
337fwrite("","");
338<?php
339$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
340// Output one line until end-of-file
341while(!feof($myfile)) {
342 echo fgets($myfile) . "<br>";
343}
344fclose($myfile);
345?>
346
347
348JAVA SORT
349<button onclick="myFunction1()">Sort Alphabetically</button>
350<button onclick="myFunction2()">Sort Numerically</button>
351
352<p id="demo"></p>
353
354<script>
355var points = [40, 100, 1, 5, 25, 10];
356document.getElementById("demo").innerHTML = points;
357
358function myFunction1() {
359 points.sort();
360 document.getElementById("demo").innerHTML = points;
361}
362function myFunction2() {
363 points.sort(function(a, b){return a - b});
364 document.getElementById("demo").innerHTML = points;
365}
366</script>
367
368alert("Hello\nHow are you?");