Reputation: 112
I'm looking to password protect a specific page on my site (index.html) with PHP code.
Here's what I have right now (this code is C/P from another question on here):
<?php
session_start();
$username="admin";
$password="jATPqhu9";
if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
header("Location: ecks.html");
exit;
}
else
{
?>
<html>
<head>
</head>
<body>
<div style='text-align:center'>
<h3>Welcome To ---</h3>
</div>
<hr /><br />
<form id='login' action="" method='post' accept-charset='UTF-8'>
<fieldset style="width:550px">
<legend>Admin Login</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >UserName:</label>
<input type='text' name='username' id='username' maxlength="50" />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='submit' value='Submit' />
</fieldset>
</form>
</body>
</html>
<?php
}
?>
For some reason, the redirect part (header("Location: ecks.html");) won't redirect to ecks.html.
Upvotes: 2
Views: 671
Reputation: 438
I don't think there is any use of setting up user name and password in HTML or some other pages it's best to protect it from server side. to do so, in server, direct towards something like Leach Protection or directory protection or hot link and set password for that there when ever you try to access that page you will require user name and password when ever you access your file on web.
Upvotes: 1
Reputation: 883
<?php
session_start();
$username="admin";
$password="jATPqhu9";
if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
$_SESSION['username'] = $username;
header("Location: ecks.html");
exit;
}
else
{
echo "incorrect info.";
}
?>
Put this in the ecks.html page
<?php
session_start();
if (!isset($_SESSION['username'])) {
//login html
}
?>
Hope this works
Upvotes: 0