In: Computer Science
//code
if(isset($_GET['submit'])){
//sanitize the input
/* Check the error from the input:
if input from user is empty
-> get an error string variable
if input is not empty
-> use preg_match() to match the pattern
$pattern = "/^[1-9][0-9]{2}(\.|\-)[0-9]{3}(\.|\-)[0-9]{4}$/";
-> if it's a matched, get a success string variable
*/
}
?>
Lab 2-Part1
Phone Number
>
xxx.xxx.xxx or xxx-xxx-xxxx
Must enter a valid
phone number!
Phone number is
valid!
Submit
<?php
session_start(); //initiates the sessions
//begin testing
$_POST['user'] = 'username1';
$_POST['submit'] = true;
$_POST['password'] = 'pass3';
//end testing
if (isset($_POST['submit'])) //checks to make sure the login form has been submitted
{
$users = array('username1','username2','username3');
$passwords = array('pass1','pass2','pass3' );
if(in_array($_POST['user'], $users))
{
$key = array_search($_POST['user'], $users);
if($passwords[$key]==$_POST['password'])
{
$_SESSION['access'] = 1; //if login is successful create a session that can be authenticated
//header("Location: " . $_SERVER['PHP_SELF']);
echo "welcome back ".$_POST['user'];
} else //if password is incorrect reload the login form
{
//header("Location: " . $_SERVER['PHP_SELF']);
echo "Password incorrect, error, redirecting to login";
}
}
}
else
{
echo "Login form";
}
?>
Output:
Password incorrect, error, redirecting to login
But if you change the value of $_POST['password'] to 'pass1', like this:
$_POST['password'] = 'pass1';
You have this output:
welcome back username1