(table with the username and encrypted password in the data and The logout scrip
ID: 3719475 • Letter: #
Question
(table with the username and encrypted password in the data and The logout script.)
Hello, so I am trying to finish up an assignment that was requiring me to password protect my script(which I did it already). I was able to protect it creating the login.php and adding the code below to the index.php. However, my professor still wants two more things from us, that I don't know/have an idea how to do it.
The database table with the username and encrypted password in the data and The logout script. Could you guys help me?
(Below is all the code that I have until now)
(Adding this to index.php or similar:)
login.php:
Explanation / Answer
To create a database table you can use the following SQL:
CREATE TABLE users
(
uname varchar(30)
upwd varchar(50),
)
To encrypt password using php,you can use the most popular PHP md5() function which generates
a hash out of the provided string to it.Other functions are:hash(),sha1(),crypt(),etc.Thus,whatever password is entered can be encryted by using any of the above functions and then inserted into the database by using insert query as:
INSERT INTO users VALUES ('username','password')
Example of a md5() function encryption:
<?php
$str = "Hello";
echo md5($str);
?>
OUTPUT: 8b1a9953c4611296a827abf8c47804d7
Well,the login.php is not actually visible with your question.But still,the logout functionality can be implemnted very simply.
To include a logout script in ypur project,you need to remove the session (or cookies) that you have created for the user login.
For example,if you have used Cookies to maintain the state of the user who logged in by using:
setcookie("username", $_POST["uname"], time()+7*24);
Then after the logout button gets clicked by the user on a new page,you can remove the cookie.So,he will no longer be able to visit
the website until he/she logs in again.
Disable a cookie :
setcookie("username", $_POST["uname"], time()-1);
For restricting the user to visit the website without login you can use something like:
if(isset($_COOKIE["username"]))
{
echo"Welcome ".$_POST["username"];
//rest of the code here
}
I am including a simple login and logout script here using cookies:
login.php
html>
<head>
</head>
<body>
<form action=welcome.php method=post>
Username:<input type=text name=username>
Password:<input type=password name=password>
<input type=submit name=sub value=Login>
</form>
</body>
</html>
logout.php
<?php
if(isset($_POST["sub"]))
{
setcookie("username",$_POST["username"]);
}
if(isset($_GET["sub1"]))
{
setcookie("username",time()-1);
echo"Logged out succesfully";
}
if(isset($_COOKIE["username"]))
{
echo"Welcome ".$_POST["username"];
echo"<form action=welcome.php>";
echo"<br>";
echo"<input type=submit name=sub1 value=LOGOUT>";
echo"</form>";
}
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.