Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple LOGIN username only PHP FORM! Write a PHP script that, when first loaded,

ID: 3794909 • Letter: S

Question

Simple LOGIN username only PHP FORM!

Write a PHP script that, when first loaded, presents a simple HTML login form with just a username prompt, like below: When the user enters a username, create a new PHP session and store the username in it. Ensure to sanitise the username input to remove any tags and special characters in it! Once logged in, your script should show the user a page like this. Hello user you are logged in. The user's session should remain active and the above "logged in" page should be shown even if the user refreshes the page or resubmits the form, or closes and reopens the webpage. When the user clicks the "Logout" button, their active session should be destroyed and they should be redirected to the original login form, allowing them to login again.

Explanation / Answer

Login.php

$sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";

$result=mysql_query($sql);

$row=mysql_fetch_array($result);

$active=$row['active'];

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1)

{

session_register("myusername");

$_SESSION['login_user']=$myusername;

header("location: welcome.php");

}

else

{

$error="Your Login Name or Password is invalid";

}

}

?>

<form action="" method="post">

<label>UserName :</label>

<input type="text" name="username"/><br />

<label>Password :</label>

<input type="password" name="password"/><br />

<input type="submit" value=" Submit "/><br />

</form>

Logout.php

< ?php

session_start();

if(session_destroy())

{

header("Location: login.php");

}

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote