Forum Moderators: coopster
// start session
session_start();
// convert username and password from _POST or _SESSION
if($_POST){
$_SESSION['username']=$_POST["username"];
$_SESSION['password']=$_POST["password"];
}
// query for a user/pass match
$result=mysql_query("select * from users
where username='" . $_SESSION['username'] . "' and password='" . $_SESSION['password'] . "'");
// retrieve number of rows resulted
$num=mysql_num_rows($result);
// print login form and exit if failed.
if($num < 1){
echo "You are not authenticated. Please login.<br><br>
<form method=POST action=index.php>
username: <input type=text name=\"username\">
password: <input type=password name=\"password\">
<input type=submit>
</form>";
exit;
}
?> [/php]
And this is a sample page:
[php]
<?
// Login & Session example by sde
// link_1.php
// connect to database
include("inc/connect.php");
// include auth and nav
include("inc/auth.php");
// begin content
include("inc/nav.php");
echo "This is my Link 1.";
// close mysql connection
mysql_close();
?> [/php]
For my page to be password protected it has to go within the echo""; but I cannot put php code here, therefore I can't password protect my php pages with this code.
Any suggestions of how to get around this?
Many thanks
Wendy
For my page to be password protected it has to go within the echo""; but I cannot put php code here
This is kind of confusing. "It" what has to go within the echo? What has to go within the echo, and why? And why can't you put php code there? It's within a php block.
I'll attempt to answer what I think you're asking. It looks to me like all you need to do is replace the line echo "This is my Link 1."; with whatever content you want your password-protected page to contain. If you want to drop back into html for the content, just close your php tag, drop in your content, put a new opening php tag following the content:
include("inc/nav.php");
?>HTML PAGE CONTENT HERE
<?php
// close mysql connection
mysql_close();