In: Computer Science
Verify Login Page that either gives error when not meeting the requirement or directing it to "blogs.php" when it's a successful log in..
Hi, so this is actually my html code for the log in and sign up page and I just needed help creating a verify log in/ sign up page with exception handling. Username has to be at least 6 character long. Password has to be 6 characters long and end with a number.
Blogs.com
"
"index.html"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Blogs.com</title>
</head>
<body>
<h2>Sign in </h2>
<p> Enter your username and password to sign
in!</p>
<form method="POST" action ="blogs.php">
<p> User Name <input type ="text"
name="username"/></p>
<p> Password <input type="text" name ="passwordname"
/></p>
<input type="submit" value= "Sign in"/></p>
</form>
<p> Sign up if you're a first time user!</p>
<form method="POST" action ="UserRegistration.php">
<input type="submit" value= "Sign up!"/></p>
<br /><br />
<script>
var date =new Date();
document.write("Today " ,date);
</br>
</script>
</body>
</html>
"UserRegistration"
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>User Registration</title>
<meta
http-equiv="content-type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<h1>User
Registration</h1>
<h2>Register / Log In</h2>
<p>New
user's, please complete the top form to register as a new user.
Returning user's, please complete
the second form to log in.</p>
<hr
/>
<h3>User
Registration</h3>
<form
method="post" action="Register.php?<?php echo SID;
?>">
<p>Enter your Name:
First <input type="text"
name="first" />
Last <input type="text"
name="last" />
</p>
<p>Enter your e-mail address:
<input type="text"
name="email" />
</p>
<p>Enter your password:
<input type="password"
name="password" />
</p>
<p>Confirm your password:
<input type="password"
name="password2" />
</p>
<p>
<em>(Passwords are
case-sensitive and must be at least 6 characters
long)</em>
</p>
<input type="reset" name="reset" value="Reset
Registration Form" />
<input type="submit" name="register"
value="Register" />
</form>
<hr
/>
<?php
$nag_counter = 0;
if(isset($_COOKIE['userVisit']))
$UserVisit = "<p>Your
visit number is $nag_counter was on " .
$_COOKIE['userVisit'];
else
$UserVisit = "<p>This
is your first visit!</p>\n";
++$nag_counter;
setcookie("userVisit", date("F j, Y, g:i a"),
time()+60*60*24*365);
?>
<?php
echo $UserVisit;
?>
</body>
</html>
"
For this example we must use the sessions to handle each thing in right manner.
// index.php
<?php
session_start();
$session = $_SESSION;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Blogs.com</title>
</head>
<body>
<h2>Sign in </h2>
<div>
<?php
if(isset($session['errors'])
&& count($session['errors'])){
foreach
($session['errors'] as $error) {
echo '<p
style="color:red">'.$error.'</p>';
}
session_destroy();
}
?>
</div>
<p> Enter your username and password to sign
in!</p>
<form method="POST" action ="blogs.php">
<p> User Name <input type ="text"
name="username"/></p>
<p> Password <input type="text" name ="passwordname"
/></p>
<input type="submit" name="submit" value= "Sign
in"/></p>
</form>
<p> Sign up if you're a first time user!</p>
<form method="POST" action ="UserRegistration.php">
<input type="submit" value= "Sign up!"/></p>
<br /><br />
<script>
var date =new Date();
document.write("Today " ,date);
</script>
</br>
</body>
</html>
***********************************************************************
// blogs.php
<?php
session_start();
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['passwordname'];
$errors = [];
if(strlen($username) < 6 ){
array_push($errors,"Username must
be 6 character long");
}
if(strlen($password) < 6 || !preg_match("/[0-9]$/",
$password)){
array_push($errors,"Password must
be 6 character long & must end with number");
}
if(count($errors)){
$_SESSION['errors'] =
$errors;
header("location:
index.php");
}
echo "Successfully passed all the cases";
}
********************************************************
// UserRegistration.php
<?php
session_start();
$session = $_SESSION;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>User Registration</title>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<h1>User Registration</h1>
<h2>Register / Log In</h2>
<p>New user's, please complete the top form to register as a
new user. Returning user's, please complete
the second form to log in.</p>
<hr />
<h3>User Registration</h3>
<div>
<?php
if(isset($session['errors']) &&
count($session['errors'])){
foreach ($session['errors'] as $error) {
echo '<p style="color:red">'.$error.'</p>';
}
session_destroy();
}
?>
</div>
<form method="post" action="Register.php">
<p>Enter your Name:
First <input type="text" name="first" />
Last <input type="text" name="last" />
</p>
<p>Enter your e-mail address:
<input type="text" name="email" />
</p>
<p>Enter your password:
<input type="password" name="password" />
</p>
<p>Confirm your password:
<input type="password" name="password2" />
</p>
<p>
<em>(Passwords are case-sensitive and must be at least 6
characters long)</em>
</p>
<input type="reset" name="reset" value="Reset Registration Form"
/>
<input type="submit" name="register" value="Register"
/>
</form>
<hr />
<?php
$nag_counter = 0;
if(isset($_COOKIE['userVisit']))
$UserVisit = "<p>Your visit number is $nag_counter was on " .
$_COOKIE['userVisit'];
else
$UserVisit = "<p>This is your first
visit!</p>\n";
++$nag_counter;
setcookie("userVisit", date("F j, Y, g:i a"),
time()+60*60*24*365);
?>
<?php
echo $UserVisit;
?>
</body>
</html>
*******************************************************************
//register.php
<?php
session_start();
if(isset($_POST['register'])){
$password = $_POST['password'];
$password2 = $_POST['password2'];
$errors = [];
if(strlen($password) < 6 ){
array_push($errors,"Password must
be 6 character long");
}
if($password != $password2){
array_push($errors,"Password &
confirm password not matching");
}
if(count($errors)){
$_SESSION['errors'] =
$errors;
header("location:
userregistration.php");
}
echo "Successfully passed all the cases";
}
Please feel free to ask anything on this. I'll reply you comment ASAP.