<?php
/*********************************************
Write a script to enter urls using a form and
save the unique urls in a cookie for the current
browser session.
Then display the urls that are saved using the
same script as active hyperlinks opening in a
target window.
setcookie(name,value,expire,path,domain,secure)
cookie name = urlcookie
*********************************************/
$entry = $_POST['entry'];
// Add Cookie
if(isset($_POST['submit'])){
// If theres no cookie set, set it
if(!$_COOKIE['addedEntry']){
if($entry != ''){ // If the entry isnt Blank
if(preg_match("/[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/", $entry)){ // If the entry is valid domain
setcookie("addedEntry[]", $entry); // add it to the COOKIE array
setcookie("message", ''); // Clear the message
} else {
$message = 'Not a valid Domain Name';
setcookie("message" , $message);
}
} else {
$message = 'No Domain Entered';
setcookie("message" , $message);
}
header("Location: wk9basic.php");
exit();
} else {
// If COOKIE is set
if($entry != ''){ // If the entry isn't blank
if(preg_match("/[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/", $entry)){ // IF the entry is a valid domain
$count = count($_COOKIE['addedEntry']);
$i = $count++;
setcookie("addedEntry[$i]", $entry);// Add it to the COOKIE array
setcookie("message", ''); // Clear the message
} else {
$message = 'Not a valid Domain Name';
setcookie("message" , $message);
}
} else { // If the entry is blank
$message = 'No Domain Entered';
setcookie("message" , $message);
}
header("Location: wk9basic.php");
exit();
}
}
// UNDO/Delete
if(isset($_POST['undo'])){
// Count the COOKIE array
$count = count($_COOKIE['addedEntry']);
// Remove from cookie
if($count > 1){
$count = $count -1;
setcookie("addedEntry[$count]" , "");
} else {
if($count == 1){
setcookie("addedEntry[]", "");
}
}
header("Location: wk9basic.php");
exit();
}
?>
<html>
<head>
<title>Week #9 BASIC ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<table border="0">
<tr>
<td valign="top" align="right">
<form method="post" name="myForm" action="<? $PHP_SELF ?>">
<b>Add URL:</b>
<?php
// IF theres a message, display it
if($_COOKIE['message']){
print("<span style=\"font:verdana;color:red;font-size:10px;\">$_COOKIE[message]</span>");
}
?><br />
www.<input type="text" size="30" name="entry" ><br />
<input type="submit" name="submit" value="Submit"> <input type="submit" name="undo" value="Undo">
</td>
<?php
if($_COOKIE['addedEntry']){
print("<td>");
foreach($_COOKIE['addedEntry'] as $t){
print("<a href=\"http://www.$t\" target=\"blank\">http://www.$t</a><br />");
}
print("</td>");
}
?>
</tr>
</table>
<pre>
</pre>
<br>
<iframe src='wk9basicsrc.php' width='100%' height='300'></iframe>
<br>
<a href='../index.html'>BACK</a>
</body>
</html>