First Draft

First Draft of the Example Files
This commit is contained in:
RobinNixon
2020-12-10 13:21:38 +00:00
parent 77a7b57c4f
commit 0335342e00
1127 changed files with 46959 additions and 56 deletions
+1
View File
@@ -0,0 +1 @@
Please note that there are no examples in this folder
+14
View File
@@ -0,0 +1,14 @@
<?php // login.php
$host = 'localhost'; // Change as necessary
$data = 'publications'; // Change as necessary
$user = 'root'; // Change as necessary
$pass = 'mysql'; // Change as necessary
$chrs = 'utf8mb4';
$attr = "mysql:host=$host;dbname=$data;charset=$chrs";
$opts =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
?>
+12
View File
@@ -0,0 +1,12 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
+4
View File
@@ -0,0 +1,4 @@
<?php
$query = "SELECT * FROM classics";
$result = $pdo->query($query);
?>
+24
View File
@@ -0,0 +1,24 @@
<?php // query-pdo.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "SELECT * FROM classics";
$result = $pdo->query($query);
while ($row = $result->fetch())
{
echo 'Author: ' . htmlspecialchars($row['author']) . "<br>";
echo 'Title: ' . htmlspecialchars($row['title']) . "<br>";
echo 'Category: ' . htmlspecialchars($row['categoryr']) . "<br>";
echo 'Year: ' . htmlspecialchars($row['year']) . "<br>";
echo 'ISBN: ' . htmlspecialchars($row['isbn']) . "<br><br>";
}
?>
+24
View File
@@ -0,0 +1,24 @@
<?php //fetchrow.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "SELECT * FROM classics";
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_BOTH)) // Style of fetch
{
echo 'Author: ' . htmlspecialchars($row['author']) . "<br>";
echo 'Title: ' . htmlspecialchars($row['title']) . "<br>";
echo 'Category: ' . htmlspecialchars($row['categoryr']) . "<br>";
echo 'Year: ' . htmlspecialchars($row['year']) . "<br>";
echo 'ISBN: ' . htmlspecialchars($row['isbn']) . "<br><br>";
}
?>
+78
View File
@@ -0,0 +1,78 @@
<?php // sqltest.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post($pdo, 'isbn');
$query = "DELETE FROM classics WHERE isbn=$isbn";
$result = $pdo->query($query);
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post($pdo, 'author');
$title = get_post($pdo, 'title');
$category = get_post($pdo, 'category');
$year = get_post($pdo, 'year');
$isbn = get_post($pdo, 'isbn');
$query = "INSERT INTO classics VALUES" .
"($author, $title, $category, $year, $isbn)";
$result = $pdo->query($query);
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author">
Title <input type="text" name="title">
Category <input type="text" name="category">
Year <input type="text" name="year">
ISBN <input type="text" name="isbn">
<input type="submit" value="ADD RECORD">
</pre></form>
_END;
$query = "SELECT * FROM classics";
$result = $pdo->query($query);
while ($row = $result->fetch())
{
$r0 = htmlspecialchars($row['author']);
$r1 = htmlspecialchars($row['title']);
$r2 = htmlspecialchars($row['category']);
$r3 = htmlspecialchars($row['year']);
$r4 = htmlspecialchars($row['isbn']);
echo <<<_END
<pre>
Author $r0
Title $r1
Category $r2
Year $r3
ISBN $r4
</pre>
<form action='sqltest.php' method='post'>
<input type='hidden' name='delete' value='yes'>
<input type='hidden' name='isbn' value='$r4'>
<input type='submit' value='DELETE RECORD'></form>
_END;
}
function get_post($pdo, $var)
{
return $pdo->quote($_POST[$var]);
}
?>
+22
View File
@@ -0,0 +1,22 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "CREATE TABLE cats (
id SMALLINT NOT NULL AUTO_INCREMENT,
family VARCHAR(32) NOT NULL,
name VARCHAR(32) NOT NULL,
age TINYINT NOT NULL,
PRIMARY KEY (id)
)";
$result = $pdo->query($query);
?>
+27
View File
@@ -0,0 +1,27 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "DESCRIBE cats";
$result = $pdo->query($query);
echo "<table><tr><th>Column</th><th>Type</th><th>Null</th><th>Key</th></tr>";
while ($row = $result->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
for ($k = 0 ; $k < 4 ; ++$k)
echo "<td>" . htmlspecialchars($row[$k]) . "</td>";
echo "</tr>";
}
echo "</table>";
?>
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "DROP TABLE cats";
$result = $pdo->query($query);
?>
+19
View File
@@ -0,0 +1,19 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "INSERT INTO cats VALUES(NULL, 'Lion', 'Leo', 4)";
$result = $pdo->query($query);
$query = "INSERT INTO cats VALUES(NULL, 'Cougar', 'Growler', 2)";
$result = $pdo->query($query);
$query = "INSERT INTO cats VALUES(NULL, 'Cheetah', 'Charly', 3)";
$result = $pdo->query($query);
?>
+27
View File
@@ -0,0 +1,27 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "SELECT * FROM cats";
$result = $pdo->query($query);
echo "<table><tr> <th>Id</th> <th>Family</th><th>Name</th><th>Age</th></tr>";
while ($row = $result->fetch(PDO::FETCH_NUM))
{
echo "<tr>";
for ($k = 0 ; $k < 4 ; ++$k)
echo "<td>" . htmlspecialchars($row[$k]) . "</td>";
echo "</tr>";
}
echo "</table>";
?>
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "UPDATE cats SET name='Charlie' WHERE name='Charly'";
$result = $pdo->query($query);
?>
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "DELETE FROM cats WHERE name='Growler'";
$result = $pdo->query($query);
?>
+17
View File
@@ -0,0 +1,17 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "INSERT INTO cats VALUES(NULL, 'Lynx', 'Stumpy', 5)";
$result = $pdo->query($query);
echo "The Insert ID was: " . $pdo->lastInsertId();
?>
+32
View File
@@ -0,0 +1,32 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "SELECT * FROM customers";
$result = $pdo->query($query);
while ($row = $result->fetch())
{
$custname = htmlspecialchars($row['name']);
$custisbn = htmlspecialchars($row['isbn']);
echo "$custname purchased ISBN $custisbn: <br>";
$subquery = "SELECT * FROM classics WHERE isbn='$custisbn'";
$subresult = $pdo->query($subquery);
$subrow = $subresult->fetch();
$custbook = htmlspecialchars($subrow['title']);
$custauth = htmlspecialchars($subrow['author']);
echo "&nbsp;&nbsp; '$custbook' by $custauth<br><br>";
}
?>
+7
View File
@@ -0,0 +1,7 @@
<?php
function mysql_fix_string($pdo, $string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return $pdo->quote($string);
}
?>
+24
View File
@@ -0,0 +1,24 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$user = mysql_fix_string($pdo, $_POST['user']);
$pass = mysql_fix_string($pdo, $_POST['pass']);
$query = "SELECT * FROM users WHERE user=$user AND pass=$pass";
// Etc...
function mysql_fix_string($pdo, $string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return $pdo->quote($string);
}
?>
+10
View File
@@ -0,0 +1,10 @@
PREPARE statement FROM "INSERT INTO classics VALUES(?,?,?,?,?)";
SET @author = "Emily Brontë",
@title = "Wuthering Heights",
@category = "Classic Fiction",
@year = "1847",
@isbn = "9780553212587";
EXECUTE statement USING @author,@title,@category,@year,@isbn;
DEALLOCATE PREPARE statement;
+28
View File
@@ -0,0 +1,28 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$stmt = $pdo->prepare('INSERT INTO classics VALUES(?,?,?,?,?)');
$stmt->bindParam(1, $author, PDO::PARAM_STR, 128);
$stmt->bindParam(2, $title, PDO::PARAM_STR, 128);
$stmt->bindParam(3, $category, PDO::PARAM_STR, 16 );
$stmt->bindParam(4, $year, PDO::PARAM_INT );
$stmt->bindParam(5, $isbn, PDO::PARAM_STR, 13 );
$author = 'Emily Brontë';
$title = 'Wuthering Heights';
$category = 'Classic Fiction';
$year = '1847';
$isbn = '9780553212587';
$stmt->execute([$author, $title, $category, $year, $isbn]);
printf("%d Row inserted.\n", $stmt->rowCount());
?>
+12
View File
@@ -0,0 +1,12 @@
<?php
function mysql_entities_fix_string($pdo, $string)
{
return htmlentities(mysql_fix_string($pdo, $string));
}
function mysql_fix_string($pdo, $string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return $pdo->real_escape_string($string);
}
?>
+29
View File
@@ -0,0 +1,29 @@
<?php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$user = mysql_entities_fix_string($pdo, $_POST['user']);
$pass = mysql_entities_fix_string($pdo, $_POST['pass']);
$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
//Etc…
function mysql_entities_fix_string($pdo, $string)
{
return htmlentities(mysql_fix_string($pdo, $string));
}
function mysql_fix_string($pdo, $string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return $pdo->quote($string);
}
?>
+14
View File
@@ -0,0 +1,14 @@
<?php // login.php
$host = 'localhost';
$data = 'publications';
$user = 'root'; // Change as necessary
$pass = 'mysql'; // Change as necessary
$chrs = 'utf8mb4';
$attr = "mysql:host=$host;dbname=$data;charset=$chrs";
$opts =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
?>
+78
View File
@@ -0,0 +1,78 @@
<?php // sqltest.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post($pdo, 'isbn');
$query = "DELETE FROM classics WHERE isbn=$isbn";
$result = $pdo->query($query);
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post($pdo, 'author');
$title = get_post($pdo, 'title');
$category = get_post($pdo, 'category');
$year = get_post($pdo, 'year');
$isbn = get_post($pdo, 'isbn');
$query = "INSERT INTO classics VALUES" .
"($author, $title, $category, $year, $isbn)";
$result = $pdo->query($query);
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author">
Title <input type="text" name="title">
Category <input type="text" name="category">
Year <input type="text" name="year">
ISBN <input type="text" name="isbn">
<input type="submit" value="ADD RECORD">
</pre></form>
_END;
$query = "SELECT * FROM classics";
$result = $pdo->query($query);
while ($row = $result->fetch())
{
$r0 = htmlspecialchars($row['author']);
$r1 = htmlspecialchars($row['title']);
$r2 = htmlspecialchars($row['category']);
$r3 = htmlspecialchars($row['year']);
$r4 = htmlspecialchars($row['isbn']);
echo <<<_END
<pre>
Author $r0
Title $r1
Category $r2
Year $r3
ISBN $r4
</pre>
<form action='sqltest.php' method='post'>
<input type='hidden' name='delete' value='yes'>
<input type='hidden' name='isbn' value='$r4'>
<input type='submit' value='DELETE RECORD'></form>
_END;
}
function get_post($pdo, $var)
{
return $pdo->quote($_POST[$var]);
}
?>
+16
View File
@@ -0,0 +1,16 @@
<?php // formtest.php
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="post" action="formtest.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
+20
View File
@@ -0,0 +1,20 @@
<?php // formtest2.php
if (!empty(($_POST['name']))) $name = $_POST['name'];
else $name = "(Not Entered)";
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your name is: $name<br>
<form method="post" action="formtest.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
+7
View File
@@ -0,0 +1,7 @@
<form method="post" action="calc.php"><pre>
Loan Amount <input type="text" name="principle">
Monthly Repayment <input type="text" name="monthly">
Number of Years <input type="text" name="years" value="25">
Interest Rate <input type="text" name="rate" value="6">
<input type="submit">
</pre></form>
+4
View File
@@ -0,0 +1,4 @@
Vanilla <input type="checkbox" name="ice" value="Vanilla">
Chocolate <input type="checkbox" name="ice" value="Chocolate">
Strawberry <input type="checkbox" name="ice" value="Strawberry">
+3
View File
@@ -0,0 +1,3 @@
Vanilla <input type="checkbox" name="ice[]" value="Vanilla">
Chocolate <input type="checkbox" name="ice[]" value="Chocolate">
Strawberry <input type="checkbox" name="ice[]" value="Strawberry">
+3
View File
@@ -0,0 +1,3 @@
8am-Noon<input type="radio" name="time" value="1">
Noon-4pm<input type="radio" name="time" value="2" checked="checked">
4pm-8pm<input type="radio" name="time" value="3">
+8
View File
@@ -0,0 +1,8 @@
Vegetables
<select name="veg" size="1">
<option value="Peas">Peas</option>
<option value="Beans">Beans</option>
<option value="Carrots">Carrots</option>
<option value="Cabbage">Cabbage</option>
<option value="Broccoli">Broccoli</option>
</select>
+8
View File
@@ -0,0 +1,8 @@
Vegetables
<select name="veg" size="5" multiple="multiple">
<option value="Peas">Peas</option>
<option value="Beans">Beans</option>
<option value="Carrots">Carrots</option>
<option value="Cabbage">Cabbage</option>
<option value="Broccoli">Broccoli</option>
</select>
+17
View File
@@ -0,0 +1,17 @@
<?php
function sanitizeString($var)
{
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = strip_tags($var);
$var = htmlentities($var);
return $var;
}
function sanitizeMySQL($pdo, $var)
{
$var = $pdo->quote($var);
$var = sanitizeString($var);
return $var;
}
?>
+47
View File
@@ -0,0 +1,47 @@
<?php // convert.php
$f = $c = '';
if (isset($_POST['f'])) $f = sanitizeString($_POST['f']);
if (isset($_POST['c'])) $c = sanitizeString($_POST['c']);
if (is_numeric($f))
{
$c = intval((5 / 9) * ($f - 32));
$out = "$f &deg;f equals $c &deg;c";
}
elseif(is_numeric($c))
{
$f = intval((9 / 5) * $c + 32);
$out = "$c &deg;c equals $f &deg;f";
}
else $out = "";
echo <<<_END
<html>
<head>
<title>Temperature Converter</title>
</head>
<body>
<pre>
Enter either Fahrenheit or Celsius and click on Convert
<b>$out</b>
<form method="post" action="convert.php">
Fahrenheit <input type="text" name="f" size="7">
Celsius <input type="text" name="c" size="7">
<input type="submit" value="Convert">
</form>
</pre>
</body>
</html>
_END;
function sanitizeString($var)
{
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
+47
View File
@@ -0,0 +1,47 @@
<?php // convert.php
$f = $c = '';
if (isset($_POST['f'])) $f = sanitizeString($_POST['f']);
if (isset($_POST['c'])) $c = sanitizeString($_POST['c']);
if (is_numeric($f))
{
$c = intval((5 / 9) * ($f - 32));
$out = "$f &deg;f equals $c &deg;c";
}
elseif(is_numeric($c))
{
$f = intval((9 / 5) * $c + 32);
$out = "$c &deg;c equals $f &deg;f";
}
else $out = "";
echo <<<_END
<html>
<head>
<title>Temperature Converter</title>
</head>
<body>
<pre>
Enter either Fahrenheit or Celsius and click on Convert
<b>$out</b>
<form method="post" action="convert.php">
Fahrenheit <input type="text" name="f" size="7">
Celsius <input type="text" name="c" size="7">
<input type="submit" value="Convert">
</form>
</pre>
</body>
</html>
_END;
function sanitizeString($var)
{
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
+16
View File
@@ -0,0 +1,16 @@
<?php // formtest.php
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="post" action="formtest.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
+20
View File
@@ -0,0 +1,20 @@
<?php // formtest2.php
if (!empty(($_POST['name']))) $name = $_POST['name'];
else $name = "(Not Entered)";
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your name is: $name<br>
<form method="post" action="formtest2.php">
What is your name?
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
_END;
?>
+14
View File
@@ -0,0 +1,14 @@
<?php
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
echo "Welcome User: " . htmlspecialchars($_SERVER['PHP_AUTH_USER']) .
" Password: " . htmlspecialchars($_SERVER['PHP_AUTH_PW']);
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die("Please enter your username and password");
}
?>
+19
View File
@@ -0,0 +1,19 @@
<?php
$username = 'admin';
$password = 'letmein';
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
if ($_SERVER['PHP_AUTH_USER'] === $username &&
$_SERVER['PHP_AUTH_PW'] === $password)
echo "You are now logged in";
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
?>
+49
View File
@@ -0,0 +1,49 @@
<?php //setupusers.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "CREATE TABLE users (
forename VARCHAR(32) NOT NULL,
surname VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";
$result = $pdo->query($query);
$forename = 'Bill';
$surname = 'Smith';
$username = 'bsmith';
$password = 'mysecret';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
$forename = 'Pauline';
$surname = 'Jones';
$username = 'pjones';
$password = 'acrobat';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
function add_user($pdo, $fn, $sn, $un, $pw)
{
$stmt = $pdo->prepare('INSERT INTO users VALUES(?,?,?,?)');
$stmt->bindParam(1, $fn, PDO::PARAM_STR, 32);
$stmt->bindParam(2, $sn, PDO::PARAM_STR, 32);
$stmt->bindParam(3, $un, PDO::PARAM_STR, 32);
$stmt->bindParam(4, $pw, PDO::PARAM_STR, 255);
$stmt->execute([$fn, $sn, $un, $pw]);
}
?>
+46
View File
@@ -0,0 +1,46 @@
<?php // authenticate.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+54
View File
@@ -0,0 +1,54 @@
<?php // authenticate2.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
{
session_start();
$_SESSION['forename'] = $fn;
$_SESSION['surname'] = $sn;
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
die ("<p><a href='continue.php'>Click here to continue</a></p>");
}
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+13
View File
@@ -0,0 +1,13 @@
<?php // continue.php
session_start();
if (isset($_SESSION['forename']))
{
$forename = htmlspecialchars($_SESSION['forename']);
$surname = htmlspecialchars($_SESSION['surname']);
echo "Welcome back $forename.<br>
Your full name is $forename $surname.<br>";
}
else echo "Please <a href='authenticate2.php'>Click Here</a> to log in.";
?>
+9
View File
@@ -0,0 +1,9 @@
<?php
function destroy_session_and_data()
{
session_start();
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+23
View File
@@ -0,0 +1,23 @@
<?php // continue.php = version 2
session_start();
if (isset($_SESSION['forename']))
{
$forename = $_SESSION['forename'];
$surname = $_SESSION['surname'];
destroy_session_and_data();
echo htmlspecialchars("Welcome back $forename");
echo "<br>";
echo htmlspecialchars("Your full name is $forename $surname.");
}
else echo "Please <a href='authenticate.php'>click here</a> to log in.";
function destroy_session_and_data()
{
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+8
View File
@@ -0,0 +1,8 @@
<?php // sessiontest.php
session_start();
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+14
View File
@@ -0,0 +1,14 @@
<?php
session_start();
if (!isset($_SESSION['initiated']))
{
session_regenerate_id();
$_SESSION['initiated'] = 1;
}
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+45
View File
@@ -0,0 +1,45 @@
<?php // authenticate.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
echo htmlspecialchars("$fn $sn : Hi $fn, you are now logged in as '$un'");
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+54
View File
@@ -0,0 +1,54 @@
<?php // authenticate2.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = sanitise($pdo, $_SERVER['PHP_AUTH_USER']);
$pw_temp = sanitise($pdo, $_SERVER['PHP_AUTH_PW']);
$query = "SELECT * FROM users WHERE username=$un_temp";
$result = $pdo->query($query);
if (!$result->rowCount()) die("User not found");
$row = $result->fetch();
$fn = $row['forename'];
$sn = $row['surname'];
$un = $row['username'];
$pw = $row['password'];
if (password_verify(str_replace("'", "", $pw_temp), $pw))
{
session_start();
$_SESSION['forename'] = $fn;
$_SESSION['surname'] = $sn;
echo htmlspecialchars("$fn $sn : Hi $fn,
you are now logged in as '$un'");
die ("<p><a href='continue.php'>Click here to continue</a></p>");
}
else die("Invalid username/password combination");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Area"');
header('HTTP/1.0 401 Unauthorized');
die ("Please enter your username and password");
}
function sanitise($pdo, $str)
{
$str = htmlentities($str);
return $pdo->quote($str);
}
?>
+13
View File
@@ -0,0 +1,13 @@
<?php // continue.php
session_start();
if (isset($_SESSION['forename']))
{
$forename = htmlspecialchars($_SESSION['forename']);
$surname = htmlspecialchars($_SESSION['surname']);
echo "Welcome back $forename.<br>
Your full name is $forename $surname.<br>";
}
else echo "Please <a href='authenticate2.php'>Click Here</a> to log in.";
?>
+23
View File
@@ -0,0 +1,23 @@
<?php // continue.php = version 2
session_start();
if (isset($_SESSION['forename']))
{
$forename = $_SESSION['forename'];
$surname = $_SESSION['surname'];
destroy_session_and_data();
echo htmlspecialchars("Welcome back $forename");
echo "<br>";
echo htmlspecialchars("Your full name is $forename $surname.");
}
else echo "Please <a href='authenticate.php'>click here</a> to log in.";
function destroy_session_and_data()
{
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
?>
+46
View File
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Using JavaScript Cookies</title>
<script>
function SaveCookie(name, value, seconds, path, domain, secure)
{
var date = new Date()
date.setTime(date.getTime() + seconds * 1000)
var expires = seconds ? ';expires=' + date.toGMTString() : ''
path = path ? ';path=' + path : ''
domain = domain ? ';domain=' + domain : ''
secure = secure ? ';secure' : ''
document.cookie = name + '=' + escape(value) + expires + path + domain + secure
}
function ReadCookie(name)
{
var dc = ';' + document.cookie
var start = dc.indexOf(';' + name + '=')
if (start == -1) return false
start += name.length + 1
var end = dc.indexOf(';', start)
end = (end == -1) ? dc.length : end
return unescape(dc.substring(start, end))
}
function DeleteCookie(name)
{
SaveCookie(name, '', -60)
}
</script>
</head>
<body>
<p>The first time this page loads no cookie should have been set and the alert window should show that the cookie with the name <b>test</b> has the value <i>false</i> (meaning it is not set).</p>
<p>After you click OK a value is assigned to the cookie <b>test</b>. To see this new cookie's value click Reload.</p>
<script>
alert("The value of the cookie 'test' is: " + ReadCookie('test'))
SaveCookie('test', 'I love cookies')
</script>
</body>
</html>
+14
View File
@@ -0,0 +1,14 @@
<?php // login.php
$host = 'localhost';
$data = 'publications';
$user = 'root'; // Change as necessary
$pass = 'mysql'; // Change as necessary
$chrs = 'utf8mb4';
$attr = "mysql:host=$host;dbname=$data;charset=$chrs";
$opts =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
?>
+16
View File
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Using Cookies</title>
</head>
<body>
<p>The first time this page loads no cookie should have been set and the message below should show that the cookie with the name <b>test</b> has the value <i>false</i> (meaning it is not set).</p>
<p>But then a value is assigned to the cookie <b>test</b>. To see this new cookie's value click Reload.</p>
<?php
$test = 'false';
if (isset($_COOKIE['test'])) $test = $_COOKIE['test'];
echo "<p><b>The value of the cookie 'test' is: $test</b></p>";
setcookie('test', 'I love cookies');
?>
</body>
</html>
+8
View File
@@ -0,0 +1,8 @@
<?php // sessiontest.php
session_start();
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'];
?>
+49
View File
@@ -0,0 +1,49 @@
<?php //setupusers.php
require_once 'login.php';
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (\PDOException $e)
{
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$query = "CREATE TABLE users (
forename VARCHAR(32) NOT NULL,
surname VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";
$result = $pdo->query($query);
$forename = 'Bill';
$surname = 'Smith';
$username = 'bsmith';
$password = 'mysecret';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
$forename = 'Pauline';
$surname = 'Jones';
$username = 'pjones';
$password = 'acrobat';
$hash = password_hash($password, PASSWORD_DEFAULT);
add_user($pdo, $forename, $surname, $username, $hash);
function add_user($pdo, $fn, $sn, $un, $pw)
{
$stmt = $pdo->prepare('INSERT INTO users VALUES(?,?,?,?)');
$stmt->bindParam(1, $fn, PDO::PARAM_STR, 32);
$stmt->bindParam(2, $sn, PDO::PARAM_STR, 32);
$stmt->bindParam(3, $un, PDO::PARAM_STR, 32);
$stmt->bindParam(4, $pw, PDO::PARAM_STR, 255);
$stmt->execute([$fn, $sn, $un, $pw]);
}
?>
+11
View File
@@ -0,0 +1,11 @@
<html>
<head><title>Hello World</title></head>
<body>
<script type="text/javascript">
document.write("Hello World")
</script>
<noscript>
Your browser doesn't support or has disabled JavaScript
</noscript>
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
<html>
<head><title>Hello World</title></head>
<body>
<script type="text/javascript"><!--
document.write("Hello World")
// -->
</script>
</body>
</html>
+8
View File
@@ -0,0 +1,8 @@
<html>
<head><title>Hello World</title></head>
<body>
<script type="text/javascript">
document.write("Hello World)
</script>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
Examples 04 to 07 are non-runnable illustrations of error messages
+6
View File
@@ -0,0 +1,6 @@
<script>
function product(a, b)
{
return a*b
}
</script>
+10
View File
@@ -0,0 +1,10 @@
<script>
n = '838102050' // Set 'n' to a string
document.write('n = ' + n + ', and is a ' + typeof n + '<br>')
n = 12345 * 67890; // Set 'n' to a number
document.write('n = ' + n + ', and is a ' + typeof n + '<br>')
n += ' plus some text' // Change 'n' from a number to a string
document.write('n = ' + n + ', and is a ' + typeof n + '<br>')
</script>
+6
View File
@@ -0,0 +1,6 @@
<script>
function product(a, b)
{
return a*b
}
</script>
+8
View File
@@ -0,0 +1,8 @@
<script>
function test()
{
a = 123 // Global scope
var b = 456 // Local scope
if (a == 123) var c = 789 // Local scope
}
</script>
+16
View File
@@ -0,0 +1,16 @@
<script>
test()
if (typeof a != 'undefined') document.write('a = "' + a + '"<br />')
if (typeof b != 'undefined') document.write('b = "' + b + '"<br />')
if (typeof c != 'undefined') document.write('c = "' + c + '"<br />')
function test()
{
a = 123
var b = 456
if (a == 123) var c = 789
}
</script>
+12
View File
@@ -0,0 +1,12 @@
<html>
<head>
<title>Link Test</title>
</head>
<body>
<a id="mylink" href="http://mysite.com">Click me</a><br />
<script>
url = document.links.mylink.href
document.write('The URL is ' + url)
</script>
</body>
</html>
+6
View File
@@ -0,0 +1,6 @@
<script>
function $(id)
{
return document.getElementById(id)
}
</script>
+6
View File
@@ -0,0 +1,6 @@
<script>
document.write("a: " + (42 > 3) + "<br>")
document.write("b: " + (91 < 4) + "<br>")
document.write("c: " + (8 == 2) + "<br>")
document.write("d: " + (4 < 17) + "<br>")
</script>
+9
View File
@@ -0,0 +1,9 @@
<script>
myname = "Peter"
myage = 24
document.write("a: " + 42 + "<br>") // Numeric literal
document.write("b: " + "Hi" + "<br>") // String literal
document.write("c: " + true + "<br>") // Constant literal
document.write("d: " + myname + "<br>") // String variable
document.write("e: " + myage + "<br>") // Numeric variable
</script>
+4
View File
@@ -0,0 +1,4 @@
<script>
days_to_new_year = 366 - day_number;
if (days_to_new_year < 30) document.write("It's nearly New Year")
</script>
+4
View File
@@ -0,0 +1,4 @@
<script>
month = "July"
if (month == "October") document.write("It's the fall")
</script>
+6
View File
@@ -0,0 +1,6 @@
<script>
a = 1000
b = "1000"
if (a == b) document.write("1")
if (a === b) document.write("2")
</script>
+7
View File
@@ -0,0 +1,7 @@
<script>
a = 7; b = 11
if (a > b) document.write("a is greater than b<br>")
if (a < b) document.write("a is less than b<br>")
if (a >= b) document.write("a is greater than or equal to b<br>")
if (a <= b) document.write("a is less than or equal to b<br>")
</script>
+6
View File
@@ -0,0 +1,6 @@
<script>
a = 1; b = 0
document.write((a && b) + "<br>")
document.write((a || b) + "<br>")
document.write(( !b ) + "<br>")
</script>
+3
View File
@@ -0,0 +1,3 @@
<script>
if (finished == 1 || getnext() == 1) done = 1
</script>
+4
View File
@@ -0,0 +1,4 @@
<script>
gn = getnext()
if (finished == 1 OR gn == 1) done = 1;
</script>
+9
View File
@@ -0,0 +1,9 @@
<script>
string = "The quick brown fox jumps over the lazy dog"
with (string)
{
document.write("The string is " + length + " characters<br>")
document.write("In upper case it's: " + toUpperCase())
}
</script>
+15
View File
@@ -0,0 +1,15 @@
<script>
onerror = errorHandler
document.writ("Welcome to this website") // Deliberate error
function errorHandler(message, url, line)
{
out = "Sorry, an error was encountered.\n\n";
out += "Error: " + message + "\n";
out += "URL: " + url + "\n";
out += "Line: " + line + "\n\n";
out += "Click OK to continue.\n\n";
alert(out);
return true;
}
</script>
+10
View File
@@ -0,0 +1,10 @@
<script>
try
{
request = new XMLHTTPRequest()
}
catch(err)
{
// Use a different method to create an XML HTTP Request object
}
</script>
+7
View File
@@ -0,0 +1,7 @@
<script>
if (page == "Home") document.write("You selected Home")
else if (page == "About") document.write("You selected About")
else if (page == "News") document.write("You selected News")
else if (page == "Login") document.write("You selected Login")
else if (page == "Links") document.write("You selected Links")
</script>
+20
View File
@@ -0,0 +1,20 @@
<script>
switch (page)
{
case "Home":
document.write("You selected Home")
break
case "About":
document.write("You selected About")
break
case "News":
document.write("You selected News")
break
case "Login":
document.write("You selected Login")
break
case "Links":
document.write("You selected Links")
break
}
</script>
+23
View File
@@ -0,0 +1,23 @@
<script>
switch (page)
{
case "Home":
document.write("You selected Home")
break
case "About":
document.write("You selected About")
break
case "News":
document.write("You selected News")
break
case "Login":
document.write("You selected Login")
break
case "Links":
document.write("You selected Links")
break
default:
document.write("Unrecognized selection")
break
}
</script>
+7
View File
@@ -0,0 +1,7 @@
<script>
document.write(
a <= 5 ?
"a is less than or equal to 5" :
"a is greater than 5"
)
</script>
+9
View File
@@ -0,0 +1,9 @@
<script>
counter=0
while (counter < 5)
{
document.write("Counter: " + counter + "<br>")
++counter
}
</script>
+8
View File
@@ -0,0 +1,8 @@
<script>
count = 1
do
{
document.write(count + " times 7 is " + count * 7 + "<br>")
} while (++count <= 7)
</script>
+6
View File
@@ -0,0 +1,6 @@
<script>
for (count = 1 ; count <= 12 ; ++count)
{
document.write(count + " times 12 is " + count * 12 + "<br>");
}
</script>
+14
View File
@@ -0,0 +1,14 @@
<script>
haystack = new Array()
haystack[17] = "Needle"
for (j = 0 ; j < 20 ; ++j)
{
if (haystack[j] == "Needle")
{
document.write("<br>- Found at location " + j)
break
}
else document.write(j + ", ")
}
</script>
+17
View File
@@ -0,0 +1,17 @@
<script>
haystack = new Array()
haystack[4] = "Needle"
haystack[11] = "Needle"
haystack[17] = "Needle"
for (j = 0 ; j < 20 ; ++j)
{
if (haystack[j] == "Needle")
{
document.write("<br>- Found at location " + j + "<br>")
continue
}
document.write(j + ", ")
}
</script>
+12
View File
@@ -0,0 +1,12 @@
<script>
displayItems("Dog", "Cat", "Pony", "Hamster", "Tortoise")
function displayItems(v1, v2, v3, v4, v5)
{
document.write(v1 + "<br>")
document.write(v2 + "<br>")
document.write(v3 + "<br>")
document.write(v4 + "<br>")
document.write(v5 + "<br>")
}
</script>
+7
View File
@@ -0,0 +1,7 @@
<script>
function displayItems()
{
for (j = 0 ; j < displayItems.arguments.length ; ++j)
document.write(displayItems.arguments[j] + "<br>")
}
</script>
+14
View File
@@ -0,0 +1,14 @@
<script>
document.write(fixNames("the", "DALLAS", "CowBoys"))
function fixNames()
{
var s = ""
for (j = 0 ; j < fixNames.arguments.length ; ++j)
s += fixNames.arguments[j].charAt(0).toUpperCase() +
fixNames.arguments[j].substr(1).toLowerCase() + " "
return s.substr(0, s.length-1)
}
</script>
+17
View File
@@ -0,0 +1,17 @@
<script>
words = fixNames("the", "DALLAS", "CowBoys")
for (j = 0 ; j < words.length ; ++j)
document.write(words[j] + "<br>")
function fixNames()
{
var s = new Array()
for (j = 0 ; j < fixNames.arguments.length ; ++j)
s[j] = fixNames.arguments[j].charAt(0).toUpperCase() +
fixNames.arguments[j].substr(1).toLowerCase()
return s
}
</script>
+15
View File
@@ -0,0 +1,15 @@
<script>
function User(forename, username, password)
{
this.forename = forename
this.username = username
this.password = password
this.showUser = function()
{
document.write("Forename: " + this.forename + "<br>")
document.write("Username: " + this.username + "<br>")
document.write("Password: " + this.password + "<br>")
}
}
</script>
+16
View File
@@ -0,0 +1,16 @@
<script>
function User(forename, username, password)
{
this.forename = forename
this.username = username
this.password = password
this.showUser = showUser
}
function showUser()
{
document.write("Forename: " + this.forename + "<br>")
document.write("Username: " + this.username + "<br>")
document.write("Password: " + this.password + "<br>")
}
</script>
+15
View File
@@ -0,0 +1,15 @@
<script>
function User(forename, username, password)
{
this.forename = forename
this.username = username
this.password = password
User.prototype.showUser = function()
{
document.write("Forename: " + this.forename + "<br>")
document.write("Username: " + this.username + "<br>")
document.write("Password: " + this.password + "<br>")
}
}
</script>
+9
View File
@@ -0,0 +1,9 @@
<script>
numbers = []
numbers.push("One")
numbers.push("Two")
numbers.push("Three")
for (j = 0 ; j < numbers.length ; ++j)
document.write("Element " + j + " = " + numbers[j] + "<br>")
</script>
+9
View File
@@ -0,0 +1,9 @@
<script>
balls = {"golf": "Golf balls, 6",
"tennis": "Tennis balls, 3",
"soccer": "Soccer ball, 1",
"ping": "Ping Pong balls, 1 doz"}
for (ball in balls)
document.write(ball + " = " + balls[ball] + "<br>")
</script>
+23
View File
@@ -0,0 +1,23 @@
<script>
checkerboard = Array(
Array(' ', 'o', ' ', 'o', ' ', 'o', ' ', 'o'),
Array('o', ' ', 'o', ' ', 'o', ' ', 'o', ' '),
Array(' ', 'o', ' ', 'o', ' ', 'o', ' ', 'o'),
Array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
Array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '),
Array('O', ' ', 'O', ' ', 'O', ' ', 'O', ' '),
Array(' ', 'O', ' ', 'O', ' ', 'O', ' ', 'O'),
Array('O', ' ', 'O', ' ', 'O', ' ', 'O', ' '))
document.write("<pre>")
for (j = 0 ; j < 8 ; ++j)
{
for (k = 0 ; k < 8 ; ++k)
document.write(checkerboard[j][k] + " ")
document.write("<br>")
}
document.write("</pre>")
</script>
+10
View File
@@ -0,0 +1,10 @@
<script>
pets = ["Cat", "Dog", "Rabbit", "Hamster"]
pets.forEach(output)
function output(element, index, array)
{
document.write("Element at index " + index + " has the value " +
element + "<br>")
}
</script>
+7
View File
@@ -0,0 +1,7 @@
<script>
pets = ["Cat", "Dog", "Rabbit", "Hamster"]
document.write(pets.join() + "<br>")
document.write(pets.join(' ') + "<br>")
document.write(pets.join(' : ') + "<br>")
</script>

Some files were not shown because too many files have changed in this diff Show More