Jump to content

how to password protect another page with a different page


Noskiw

Recommended Posts

<?php

function no_password($password){
$password = "bambam";

if(!$password){
	echo "Please enter the password <a href='index.php?p=blog&p=admin&p=credentials'>here</a>!";
}else{
		echo "

<form action='index.php?p=blog' method='POST'>

<table width='100%'>

    <tr>

    <td width='7%' valign='top'>
    Your Name:
    </td>

    <td valign='top'>
    <input type='text' name='name' maxlength='25' />
    </td>

    </tr>

    <tr>

    <td valign='top'>
    Your Email:
    </td>

    <td>
    <input type='text' name='email' maxlength='35' />
    </td>

    </tr>

    <tr>

    <td valign='top'>
    Your Message:
    </td>

    <td>
    <textarea cols='20' rows='2' name='message' maxlength='250'></textarea>
    <p><input type='submit' name='submit' value='Post' />
    </td>

    </tr>
    </table>

    </form>";
}
}

?>

 

there is my function for protecting it

 

<?php

$cred = $_GET['credentails'];
$p = $_GET['p'];

include "functions.php";

no_password($password);


?>

 

there is my admin page.

 

<?php

if(!$_POST['submit']){
echo "<form action='index.php?p=blog&p=admin&p=credentials' method='POST'>";
echo "Enter Credentials (password): <input type='password' name='password'>";
echo "<input type='submit' name='submit'  value='Access' />";
echo "</form>";
}else{
$password = $_POST['password'];

if(!$password){
	echo "Password Required";
}else{
	if($password != "bambam"){
		echo "Wrong Password!";
	}
}
}

if($password == "bambam"){
echo "Successful!"; echo "       <a href='index.php?p=blog&p=admin'>Place your post</a>";
}

?>

 

and there is my credentials page... what am I doing wrong?

You are making this way harder than it needs to be, consider one using one page for logging in:

 

<?
  if (isset($_POST['username']) && isset($_POST['password'])) {
    if ($_POST['username'] == 'my-username' && $_POST['password'] == 'my-password') {
      session_start();
      // set session stuff here
      $_SESSION['username'] = 'my-username';
      header('Location: admin-area.php');
    }
    $message = 'Invalid credentials';
  }
?>


<h1>Login</h1>
<?= $message ?>
<form action='this-page.php' method='post'>
  username: <input type='text' name='username' /><br />
  password: <input type='password' name='password' /><br />
  <input type='submit' />
</form>

 

Then just check for the session info in subsequent requests, just make sure to always call session_start.  I usually store session info in a database as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.