Question
Erasing cookies when shouldn't (should keep for 1 minute): I have this LoginForm file working except for one thing. When I click logout I my
Erasing cookies when shouldn't (should keep for 1 minute):
I have this LoginForm file working except for one thing. When I click "logout" I my cookies are being erased. Below is the prompt and then my code. I created user Danielle Coulter with password Seinfeld
PROMPT:
IN PHP,
Write a PHP file that will output a form containing 3 fields: username, password and Remember me! checkbox. Upon submission of the form, the code should check against MySQL table to see whether the username/password pair was correct. If so, display a welcome message and save a cookie that identifies the user to the server. On further visits to the page, the user should appear logged in, even if the browser has been closed; If not, display the message Invalid username or password followed by the same login form. The output should be one of three options:
The login form.
The invalid message and the login form, if failed login.
The welcome message containing a Log Out button, if successful login.
When user clicks Log out button, remove the cookie so that the user is logged out. Clicking the button should redirect the user to the same page, which now shows a login form.
------------index.php--------------
}else{ //echo "Cookie '" . $cookie_name . "' is set! ";
echo "Welcome " . $_COOKIE['user']. "
"; }if(isset($_POST["logout"])){ //changed to session_destroy and set cookie with negative expire time //need to work on this stuff to get the logout to forget the cookie and re-show the form session_destroy(); setcookie("user", '', time() - 1); header('Location: index.php'); }
if(isset($_POST["check"])){ $servername = 'localhost'; $dbname = 'icoolsho_dcoulter'; $username = 'icoolsho_dcoulte'; $password = '$!980-49-2554'; // Create connection $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection if ($conn->connect_error){ die("Connection failed: " . $conn->connect_error); }
$username=$_POST['user']; $password=$_POST['password'];
$sql = "SELECT id FROM login where userName = '".$username."' and passWord='".$password."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//echo "success";
if(isset($_POST['rememberme'])){ setcookie('user',$username,time()+ 60,"/"); } session_start(); $_SESSION['name'] = $myusername; header("Location: index.php");
//echo "Welcome ".$_COOKIE['user']; }else{ echo("Invalid Username/Password"); }
$conn->close(); }
?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started