简体   繁体   中英

Access Denied on Php Login

I have a buyer form, called "Buyer.php":

<form method="post" action="check_buyer.php" id="LoggingInBuyer">
    <div style="width:265px;margin:0; padding:0; float:left;">
    <label>Username:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><a href="#">Forgot Username?</span></a></label> <br />
    <input id="UserReg" style="width:250px;" type="text" name="userName" tabindex="1" class="required" /></div>
    <div style="width:265px;margin:0; padding:0; float:right;">
    <label>Password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><a href="#">Forgot Password?</span></a></label> <br />
    <input id="UserReg" style="width:250px;" type="password"  name="userPass" tabindex="2" class="required" /></div>
    <div class="clearB"> </div>
    <input type="submit" style="width:100px; margin:10px 200px;" id="UserRegSubmit" name="submit" value="Login" tabindex="3" />
</form>

A file called check_buyer.php (in the same dir):

<?php
session_start(); #recall session from index.php where user logged include()

function isLoggedIn()
{
    if(isset($_SESSION['valid']) && $_SESSION['valid'])
        header( 'Location: buyer/' ); # return true if sessions are made and login creds are valid
    echo "Invalid Username and/or Password";  
    return false;
}

require_once('../inc/db/dbc.php');

$connect = mysql_connect($h, $u, $p) or die ("Can't Connect to Database.");
mysql_select_db($db);

$LoginUserName = $_POST['userName'];
$LoginPassword = mysql_real_escape_string($_POST['userPass']);
//connect to the database here
$LoginUserName = mysql_real_escape_string($LoginUserName);
$query = "SELECT uID, uUPass, dynamSalt, uUserType FROM User WHERE uUName = '$LoginUserName';";

function validateUser($ifUserExists['uID'], $ifUserExists['uUserType']) {
    $_SESSION['valid'] = 1;
    $_SESSION['uID'] = $uID;
    $_SESSION['uUserType'] = $uUserType; // 1 for buyer - 2 for merchant
}

$result = mysql_query($query);
if(mysql_num_rows($result) < 1) //no such USER exists
{
    echo "Invalid Username and/or Password";
}
$ifUserExists = mysql_fetch_array($result, MYSQL_ASSOC);

$dynamSalt = $ifUserExists['dynamSalt'];  #get value of dynamSalt in query above
$SaltyPass = hash('sha512',$dynamSalt.$LoginPassword); #recreate originally created dynamic, unique pass

if($SaltyPass != $ifUserExists['uUPass']) # incorrect PASS
{
    echo "Invalid Username and/or Password";
}else {
    validateUser();
}
// If User *has not* logged in yet, keep on /login
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}
?>

// This is now throwing error of: Parse error: syntax error, unexpected '[', expecting ')' in on line 23 which is function validateUser($ifUserExists['uID'], $ifUserExists['uUserType']) {

and the file "index.php" in the buyer/ directory:

<?php
session_start();
if($_SESSION['uUserType']!=1)
{ 
    die("You may not view this page. Access denied.");
}

function isLoggedIn()
{
    return (isset($_SESSION['valid']) && $_SESSION['valid']);
}

//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}
?>

<?php 
    if($_SESSION['valid'] == 1){
        #echo "<a href='../logout.php'>Logout</a>";
        require_once('buyer_profile.php');
    }else{
        echo "<a href='../index.php'>Login</a>";
    }
?>

The point of this is that when a username and password is entered, the user is logged in and directed to /buyer/index.php , to the buyer portion of that website. It seems everytime I login with the dummy credentials I made to test, it just blurts out : You may not view this page. Access denied You may not view this page. Access denied . But, then if I go back by pressing back arrow in browser it has me logged in and showing a link to logout .

I did some trouble shooting: 1) Shown here, to test my sql query is fine and indeed it is. http://i.stack.imgur.com/n2b5z.png

2)Tried choing out echo 'the userid: ' . $userid; echo 'the userid: ' . $userid; before it whines about You may not view.. and it doesn't print anything.

How do I go about getting this userID ? I double checked the field names in the database and all is fine..

From a quick check, it looks like you're setting $_SESSION['uUserType'] = $userType in validateUser() , but don't seem to be passing in $userType itself to that function. So $_SESSION['uUserType'] won't be 1 , but $_SESSION['valid'] will be, because you're setting it to that in validateUser() .

I suspect you should be passing valid data in to validateUser in order to set it into the session.

eg

validateUser($ifUserExists['uID'], $ifUserExists['uUserType']);

function validateUser($uID, $uUserType) {
    $_SESSION['valid'] = 1;
    $_SESSION['uID'] = $uID;
    $_SESSION['uUserType'] = $uUserType; // 1 for buyer - 2 for merchant
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM