I am trying to create a home page. Once the user comes to the site and inputs username and password the data will get posted to a checklogin.php file where it will verify the data the user entered. Is there a way that after it checks the data and it is all good then it redirects the user to another page that is the home page? I want to do this so that my entire checklogin script is not on the home page. then also if the user is in a different part of the site, and they click home, the check login script will run again and it will fail. I understand i can use session variables to see if they have already logged in and then somehow bypass the checklogin script on the home page if they have already logged in, but is this the correct way to do this?
<?php
include'vive_fns.php';
$v_username = trim($_POST['viveuser']);
$v_password = trim($_POST['vivepass']);
if(!isset($_POST['viveuser'])|| empty($v_username)){
echo"Please enter a username"; //should change to redirect
die();
}
elseif(!isset ($v_username) || empty($v_username)) {
echo "Please enter a password"; //should change to redirect
die();
}
//if all data is entered we want to check the password
$mysqli = connect_db();
//set database query
$sql1 = "SELECT password FROM vive_user WHERE username = "."'$v_username'";
//check to make sure a result is returned
if(!$result1 = $mysqli->query($sql1)){
echo 'Could not query database. Please try again later.';//should change to redirect
die();
}
else {
$data = $result1->fetch_array(MYSQLI_NUM);
$db_pass = $data[0];
}
if($db_pass !== $v_password){
$title = 'Incorrect Login Info';
//do_html_header($title); this sets the page title
echo"Incorrect Password";//should change to redirect
die();
}
//if everything checks out need to establish user info
$title = 'Home';
do_html_header($title);
//echo"Logged In";
session_start();
$_SESSION['valid_user']=$v_username;
// at this point i want to redirect
header("Location: home.php");
exit();