[php] Password Help

technokid88

Part of a System
Joined
15 Feb 2006
Messages
741
I'm trying to put a php script to block content on a page, unless you put in your registered user name and password. I have it where if a person comes to this page they get message that says you are not logged in, and a link to log in. This will happen to ever page i put this code in to. What i want is after they get this message and log in i want them to go to the page with the blocked page logged in and want it to be displayed. Any help would be much appreciated. Thanks

Code:
<?php
$username = $_COOKIE['loggedin'];
if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href=login1.php>click here</a> to login.");
echo "You are logged in $username";
?>
This is the login.php code below.

Code:
<?php
ob_start();

include("config.php"); 

// connect to the mysql server 
$link = mysql_connect($server, $db_user, $db_pass) 
or die ("Could not connect to mysql because ".mysql_error()); 

// select the database
mysql_select_db($database) 
or die ("Could not select database because ".mysql_error()); 

$match = "select id from $table where username = '".$_POST['username']."' 
and password = '".$_POST['password']."';"; 

$qry = mysql_query($match) 
or die ("Could not match data because ".mysql_error()); 
$num_rows = mysql_num_rows($qry); 

if ($num_rows <= 0) { 
echo "Sorry, there is no username or password with: <strong>".$_POST['username']."</strong><br>"; 
echo "<a href=login1.php>Try again</a>"; 
exit;

} else { 

setcookie("loggedin", "".$_POST['username']."", time()+(3600 * 24));
setcookie("username", "".$_POST['username']."", "TRUE");
echo "Welcome: <strong>".$_POST['username']."</strong><br>"; 
echo "Continue to the <a href=members.php>Listing you were looking at.</a> section.";
}
ob_end_flush();
?>
 
Last edited:
You need to use sessions really, you can store an authorisation key in a cookie if you want to remember users across browser closures but sessions are what you need.

Take a look at SitePoints PHP Sessions tutorial
 
Hey thanks for the info, looks like the exact thing i was looking for.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie Electronic Punk Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. 🙁

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk Sazar Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,016
Messages
673,499
Members
5,630
Latest member
REALMTBENJOYER
Back