I am trying to get the login processor to match what the user types in, with the hard coded object array that contains the first name, last name, and password. I am not sure how to make the check between the login and the object array. I have 3 pages, the student.class.php, login.php, and processor.php.
I also need to use the session_start() and $_SESSION array but I am not sure how to implement this into my project. would I make a seperate session file, or include it into my processor.
Student.class.php
<?php
class Student
{
private $f_name;
private $l_name;
private $full_name;
private $password;
//Constructor method
public function __construct($f_name,$l_name,$password) {
$this->f_name = $f_name;
$this->l_name = $l_name;
$this->password = $password;
}
function get_name(){
$full_name = $this->f_name.' '.$this->l_name;
return $full_name;
}
function get_level(){
return $this->level;
}
function get_gender(){
return $this->gender;
}
function get_password(){
return $this->password;
}
}
?>
login.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once 'student.class.php';
$students = array();
$students[0] = new Student('Trey', 'Smith', 'senior', 'male', 'sailor1234');
$students[1] = new Student('Kyle', 'McAulay', 'junior', 'male', 'abc123');
$students[2] = new Student('Stacey', 'Keibler','senior','female', 'hotdawg23');
$students[3] = new Student('Lindsey', 'Mullins', 'junior','female','gonoles69');
$students[4] = new Student('Kenneth', 'Jaggers','senior', 'male', 'peterpanpan');
$students[5] = new Student('Chad', 'Endris', 'sophomore', 'male','back2thefuture');
?>
<h1>Sign up for our site!</h1>
<form method="post" action="processor.php">
<fieldset>
<label>First Name</label>
<input type="text" name="first_name"/>
<label>Last Name</label>
<input type="text" name="last_name"/>
<label>Password</label>
<input type="text" name="password"/>
</fieldset>
<br><input type="submit" value="Submit"></input></br>
</body>
</html>
processor.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once 'student.class.php';
require_once 'login.php';
function check_submit($field_to_check)
{
if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
{
return TRUE;
}
else
{
return "Please fill in the $field_to_check category!";
}
}
$errors = array();
$_POST['first_name'] = strip_tags(trim($_POST['first_name']));
$_POST['last_name'] = strip_tags(trim($_POST['last_name']));
$_POST['password'] = strip_tags(trim($_POST['password']));
if (check_submit('first_name') !== TRUE)
{
$errors[] = check_submit('first_name');
}
if (check_submit('last_name') !== TRUE)
{
$errors[] = check_submit('last_name');
}
if (check_submit('password') !== TRUE)
{
$errors[] = check_submit('password');
}
if (count($errors)> 0){
echo "<ul>";
foreach($errors as $message){
echo "<li>$message</li>";
}
echo "</ul>";
die();
}
?>
<h1>Thank you for registering <?php echo $_POST['first_name']."
".$_POST['last_name']?>!</h1>
<h2>Your password was correct!</h2>
</body>
</html>
Put session_start() at the top of any page you need to set/retrieve session data
Upon login submission, loop through your students array, testing each one against your $_POST inputs. If you get a match, save the object to a session variable such as $_SESSION[‘student’]
This isn’t a great way to do this.. ideally you wouldn’t be instantiating a new object for every student unless you’re going to use all of them later.
Also, on processor.php, you’re looking for $_POST[‘first_name’], but the name of the input field is ‘f_name’