· 9 years ago · Apr 24, 2017, 05:12 AM
1<html>
2<head>
3<script>
4
5function setCookie(cname,cvalue,exdays) {
6 var d = new Date();
7 d.setTime(d.getTime() + (exdays*24*60*60*1000));
8 var expires = "expires=" + d.toGMTString();
9 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
10}
11
12function getCookie(cname) {
13 var name = cname + "=";
14 var decodedCookie = decodeURIComponent(document.cookie);
15 var ca = decodedCookie.split(';');
16 for(var i = 0; i < ca.length; i++) {
17 var c = ca[i];
18 while (c.charAt(0) == ' ') {
19 c = c.substring(1);
20 }
21 if (c.indexOf(name) == 0) {
22 return c.substring(name.length, c.length);
23 }
24 }
25 return "";
26}
27
28function checkCookie() {
29 var user=getCookie("username");
30 if (user != "") {
31 alert("Welcome again " + user);
32 } else {
33 user = prompt("Please enter your name:","");
34 if (user != "" && user != null) {
35 setCookie("username", user, 30);
36 }
37 }
38}
39</script>
40</head>
41<body onload="checkCookie()">
42</body>
43</html>
44
45<html>
46<body>
47
48<p>A script on this page starts this clock:</p>
49
50<p id="demo"></p>
51
52<script>
53var myVar = setInterval(myTimer, 1000);
54
55function myTimer() {
56 var d = new Date();
57 document.getElementById("demo").innerHTML = d.toLocaleTimeString();
58}
59</script>
60
61</body>
62</html>
63
64<!DOCTYPE html>
65<html>
66<body>
67
68<p>Click "Try it". Wait 3 seconds. The page will alert "Hello".</p>
69<p>Click "Stop" to prevent the first function to execute.</(p>
70<p>(You must click "Stop" before the 3 seconds are up.)</p>
71
72<button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button>
73
74<button onclick="clearTimeout(myVar)">Stop it</button>
75
76<script>
77function myFunction() {
78 alert("Hello");
79}
80</script>
81</body>
82</html>
83
84<html>
85<body>
86
87<h2>JavaScript Confirm Box</h2>
88
89
90<button onclick="myFunction()">Try it</button>
91
92<p id="demo"></p>
93
94<script>
95function myFunction() {
96 var txt;
97 if (confirm("Press a button!") == true) {
98 txt = "You pressed OK!";
99 } else {
100 txt = "You pressed Cancel!";
101 }
102 document.getElementById("demo").innerHTML = txt;
103}
104</script>
105
106</body>
107</html>
108<!DOCTYPE html>
109<html>
110<body>
111
112<h2>JavaScript</h2>
113
114<h3>The window.location object</h3>
115
116<input type="button" value="Load new document" onclick="newDoc()">
117
118<script>
119function newDoc() {
120 window.location.assign("https://www.w3schools.com")
121}
122</script>
123
124</body>
125</html>
126
127<html>
128<head>
129<script>
130function validateForm() {
131 var x = document.forms["myForm"]["fname"].value;
132 if (x == "") {
133 alert("Name must be filled out");
134 return false;
135 }
136}
137</script>
138</head>
139<body>
140
141<form name="myForm" action="/action_page_post.php"
142onsubmit="return validateForm()" method="post">
143Name: <input type="text" name="fname">
144<input type="submit" value="Submit">
145</form>
146
147</body>
148</html>
149
150SQL
151<?php
152$servername = "localhost";
153$username = "username";
154$password = "password";
155$dbname = "myDB";
156
157// Create connection
158$conn = new mysqli($servername, $username, $password, $dbname);
159// Check connection
160if ($conn->connect_error) {
161 die("Connection failed: " . $conn->connect_error);
162}
163
164// sql to create table
165$sql = "CREATE TABLE MyGuests (
166id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
167firstname VARCHAR(30) NOT NULL,
168lastname VARCHAR(30) NOT NULL,
169email VARCHAR(50),
170reg_date TIMESTAMP
171)";
172
173if ($conn->query($sql) === TRUE) {
174 echo "Table MyGuests created successfully";
175} else {
176 echo "Error creating table: " . $conn->error;
177}
178
179$conn->close();
180?>
181
182
183
184$sql = "SELECT id, firstname, lastname FROM MyGuests";
185$result = $conn->query($sql);
186
187if ($result->num_rows > 0) {
188 // output data of each row
189 while($row = $result->fetch_assoc()) {
190 echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
191 }
192} else {
193 echo "0 results";
194}
195$conn->close();
196?>
197
198
199SESSIONS
200<?php
201session_start();
202?>
203<!DOCTYPE html>
204<html>
205<body>
206
207<?php
208// Echo session variables that were set on previous page
209echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
210echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
211?>
212
213</body>
214</html>
215
216login.php
217
218<html>
219<head>
220<title>Login Form</title>
221</head>
222<body>
223<h2>Login Form</h2>
224<form method="post" action="checklogin.php">
225User Id: <input type="text" name="uid"><br>
226Password: <input type="password" name="pw"><br>
227<input type="submit" value="Login">
228</form>
229</body>
230</html>
231
232checklogin.php
233
234<?php
235$uid = $_POST['uid'];
236$pw = $_POST['pw'];
237
238if($uid == 'arun' and $pw == 'arun123')
239{
240 session_start();
241 $_SESSION['sid']=session_id();
242 header("location:securepage.php");
243}
244?>
245
246securepage.php
247
248<?php
249 session_start();
250 if($_SESSION['sid']==session_id())
251 {
252 echo "Welcome to you<br>";
253 echo "<a href='logout.php'>Logout</a>";
254 }
255 else
256 {
257 header("location:login.php");
258 }
259?>
260
261logout.php
262
263<?php
264
265 echo "Logged out scuccessfully";
266
267 session_start();
268 session_destroy();
269 setcookie(PHPSESSID,session_id(),time()-1);
270
271?>
272
273
274PHP EMAIL VALIDATION
275<?php
276// define variables and set to empty values
277$nameErr = $emailErr = $genderErr = $websiteErr = "";
278$name = $email = $gender = $comment = $website = "";
279
280if ($_SERVER["REQUEST_METHOD"] == "POST") {
281 if (empty($_POST["name"])) {
282 $nameErr = "Name is required";
283 } else {
284 $name = test_input($_POST["name"]);
285 // check if name only contains letters and whitespace
286 if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
287 $nameErr = "Only letters and white space allowed";
288 }
289 }
290
291 if (empty($_POST["email"])) {
292 $emailErr = "Email is required";
293 } else {
294 $email = test_input($_POST["email"]);
295 // check if e-mail address is well-formed
296 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
297 $emailErr = "Invalid email format";
298 }
299 }
300
301 if (empty($_POST["website"])) {
302 $website = "";
303 } else {
304 $website = test_input($_POST["website"]);
305 // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
306 if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
307 $websiteErr = "Invalid URL";
308 }
309 }
310
311 if (empty($_POST["comment"])) {
312 $comment = "";
313 } else {
314 $comment = test_input($_POST["comment"]);
315 }
316
317 if (empty($_POST["gender"])) {
318 $genderErr = "Gender is required";
319 } else {
320 $gender = test_input($_POST["gender"]);
321 }
322}
323?>
324
325
326
327PHP VALIDATION
328<?php
329// define variables and set to empty values
330$nameErr = $emailErr = $genderErr = $websiteErr = "";
331$name = $email = $gender = $comment = $website = "";
332
333if ($_SERVER["REQUEST_METHOD"] == "POST") {
334 if (empty($_POST["name"])) {
335 $nameErr = "Name is required";
336 } else {
337 $name = test_input($_POST["name"]);
338 }
339
340 if (empty($_POST["email"])) {
341 $emailErr = "Email is required";
342 } else {
343 $email = test_input($_POST["email"]);
344 }
345
346 if (empty($_POST["website"])) {
347 $website = "";
348 } else {
349 $website = test_input($_POST["website"]);
350 }
351
352 if (empty($_POST["comment"])) {
353 $comment = "";
354 } else {
355 $comment = test_input($_POST["comment"]);
356 }
357
358 if (empty($_POST["gender"])) {
359 $genderErr = "Gender is required";
360 } else {
361 $gender = test_input($_POST["gender"]);
362 }
363}
364?>
365
366
367<html>
368<head>
369</head>
370<body>
371
372<?php
373// define variables and set to empty values
374$name = $email = $gender = $comment = $website = "";
375
376if ($_SERVER["REQUEST_METHOD"] == "POST") {
377 $name = test_input($_POST["name"]);
378 $email = test_input($_POST["email"]);
379 $website = test_input($_POST["website"]);
380 $comment = test_input($_POST["comment"]);
381 $gender = test_input($_POST["gender"]);
382}
383
384function test_input($data) {
385 $data = trim($data);
386 $data = stripslashes($data);
387 $data = htmlspecialchars($data);
388 return $data;
389}
390?>
391
392<h2>PHP Form Validation Example</h2>
393<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
394 Name: <input type="text" name="name">
395 <br><br>
396 E-mail: <input type="text" name="email">
397 <br><br>
398 Website: <input type="text" name="website">
399 <br><br>
400 Comment: <textarea name="comment" rows="5" cols="40"></textarea>
401 <br><br>
402 Gender:
403 <input type="radio" name="gender" value="female">Female
404 <input type="radio" name="gender" value="male">Male
405 <br><br>
406 <input type="submit" name="submit" value="Submit">
407</form>
408
409<?php
410echo "<h2>Your Input:</h2>";
411echo $name;
412echo "<br>";
413echo $email;
414echo "<br>";
415echo $website;
416echo "<br>";
417echo $comment;
418echo "<br>";
419echo $gender;
420?>
421
422</body>
423</html>
424
425
426FILE
427
428<!DOCTYPE html>
429<html>
430<body>
431
432<form action="upload.php" method="post" enctype="multipart/form-data">
433 Select image to upload:
434 <input type="file" name="fileToUpload" id="fileToUpload">
435 <input type="submit" value="Upload Image" name="submit">
436</form>
437
438</body>
439</html>
440
441<?php
442$target_dir = "uploads/";
443$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
444$uploadOk = 1;
445$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
446// Check if image file is a actual image or fake image
447if(isset($_POST["submit"])) {
448 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
449 if($check !== false) {
450 echo "File is an image - " . $check["mime"] . ".";
451 $uploadOk = 1;
452 } else {
453 echo "File is not an image.";
454 $uploadOk = 0;
455 }
456}
457// Check if file already exists
458if (file_exists($target_file)) {
459 echo "Sorry, file already exists.";
460 $uploadOk = 0;
461}
462// Check file size
463if ($_FILES["fileToUpload"]["size"] > 500000) {
464 echo "Sorry, your file is too large.";
465 $uploadOk = 0;
466}
467// Allow certain file formats
468if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
469&& $imageFileType != "gif" ) {
470 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
471 $uploadOk = 0;
472}
473// Check if $uploadOk is set to 0 by an error
474if ($uploadOk == 0) {
475 echo "Sorry, your file was not uploaded.";
476// if everything is ok, try to upload file
477} else {
478 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
479 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
480 } else {
481 echo "Sorry, there was an error uploading your file.";
482 }
483}
484?>
485fread();
486fwrite("","");
487<?php
488$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
489// Output one line until end-of-file
490while(!feof($myfile)) {
491 echo fgets($myfile) . "<br>";
492}
493fclose($myfile);
494?>
495
496
497JAVA SORT
498<button onclick="myFunction1()">Sort Alphabetically</button>
499<button onclick="myFunction2()">Sort Numerically</button>
500
501<p id="demo"></p>
502
503<script>
504var points = [40, 100, 1, 5, 25, 10];
505document.getElementById("demo").innerHTML = points;
506
507function myFunction1() {
508 points.sort();
509 document.getElementById("demo").innerHTML = points;
510}
511function myFunction2() {
512 points.sort(function(a, b){return a - b});
513 document.getElementById("demo").innerHTML = points;
514}
515</script>
516
517alert("Hello\nHow are you?");
518<?php
519setcookie("test_cookie", "test", time() + 3600, '/');
520?>
521<html>
522<body>
523
524<?php
525if(count($_COOKIE) > 0) {
526 echo "Cookies are enabled.";
527} else {
528 echo "Cookies are disabled.";
529}
530?>
531
532</body>
533</html>
534
535<?php
536$cookie_name = "user";
537$cookie_value = "John Doe";
538setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
539?>
540<html>
541<body>
542
543<?php
544if(!isset($_COOKIE[$cookie_name])) {
545 echo "Cookie named '" . $cookie_name . "' is not set!";
546} else {
547 echo "Cookie '" . $cookie_name . "' is set!<br>";
548 echo "Value is: " . $_COOKIE[$cookie_name];
549}
550?>
551
552<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
553
554</body>
555</html>
556
557<?php
558$cookie_name = "user";
559$cookie_value = "Alex Porter";
560setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
561?>
562<html>
563<body>
564
565<?php
566if(!isset($_COOKIE[$cookie_name])) {
567 echo "Cookie named '" . $cookie_name . "' is not set!";
568} else {
569 echo "Cookie '" . $cookie_name . "' is set!<br>";
570 echo "Value is: " . $_COOKIE[$cookie_name];
571}
572?>
573
574<p><strong>Note:</strong> You might have to reload the page to see the new value of the cookie.</p>
575
576</body>
577</html>
578
579delete
580<?php
581// set the expiration date to one hour ago
582setcookie("user", "", time() - 3600);
583?>
584<html>
585<body>
586
587<?php
588echo "Cookie 'user' is deleted.";
589?>
590
591</body>
592</html>