Jump to content

Recommended Posts

Alright, so I am creating a members only area of a site.

Since I couldn't find premade code to do the job, I'm pulling it together.

 

My current issue is:

 

<?php // db.php

$dbhost = 'localhost';
$dbuser = 'craighoo_craig';
$dbpass = 'xxxxxxxx';

function dbConnect($db='') {
   global $dbhost, $dbuser, $dbpass;

   $dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
       or die('The site database appears to be down.');

   if ($db!='' and !@mysql_select_db($db))
       die('The site database is unavailable.');

   return $dbcnx;
}
?>

 

Is giving me the second error (The site database is unavailable).

When I use the signup.php file, the signup is successful, the confirmation email is sent and the username is added to the database.

As soon as I go to a protected page, this error appears.

 

Code for Protected page:

<?php include 'accesscontrol.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title> Members-Only Page </title>
 <meta http-equiv="Content-Type"
   content="text/html; charset=iso-8859-1
</head>
<body>
<p>Welcome, <?=$username?>! You have entered a members-only area
  of the site. Don't you feel special?</p>
</body>
</html>

 

Code for Accesscontrol.php:

 

<?php // accesscontrol.php
include_once 'common.php';
include_once 'db.php';

session_start();

$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];

if(!isset($uid)) {
 ?>
 <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title> Please Log In for Access </title>
   <meta http-equiv="Content-Type"
     content="text/html; charset=iso-8859-1" />
 </head>
 <body>
 <h1> Login Required </h1>
 <p>You must log in to access this area of the site. If you are
    not a registered user, <a href="signup.php">click here</a>
    to sign up for instant access!</p>
 <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">
   User ID: <input type="text" name="uid" size="8" /><br />
   Password: <input type="password" name="pwd" SIZE="8" /><br />
   <input type="submit" value="Log in" />
 </form></p>
 </body>
 </html>
 <?php
 exit;
}

$_SESSION['uid'] = $uid;
$_SESSION['pwd'] = $pwd;

dbConnect("sessions");
$sql = "SELECT * FROM user WHERE
       userid = '$uid' AND password = PASSWORD('$pwd')";
$result = mysql_query($sql);
if (!$result) {
 error('A database error occurred while checking your '.
       'login details.\\nIf this error persists, please '.
       'contact you@example.com.');
}

if (mysql_num_rows($result) == 0) {
 unset($_SESSION['uid']);
 unset($_SESSION['pwd']);
 ?>
 <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title> Access Denied </title>
   <meta http-equiv="Content-Type"
     content="text/html; charset=iso-8859-1" />
 </head>
 <body>
 <h1> Access Denied </h1>
 <p>Your user ID or password is incorrect, or you are not a
    registered user on this site. To try logging in again, click
    <a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To register for instant
    access, click <a href="signup.php">here</a>.</p>
 </body>
 </html>
 <?php
 exit;
}

$username = mysql_result($result,0,'fullname');
?>

 

As you can see, it's a very simple login system, but I can't seem to figure out this one issue.

The very first time I visited protectedpage.php it prompted for username and password, which I entered, and then it went to this error ever since. Does this mean that the login worked correctly but for some reason it has nowhere to go now?

 

Thanks in advance!

 

 

EDIT: Sorry for the inconvience, but I managed to find the error myself.

I accidentally used the wrong access file, which listed the database as sessions, instead of craighoo_sessions.

Doh.

Link to comment
https://forums.phpfreaks.com/topic/156009-solved-database-error-inconsistency/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.