Page 1 of 1

Access Levels

Posted: Fri Aug 30, 2013 2:43 pm
by Epiales
Okay, I always spend hours trying before I jump in here. Gets frustrating...

What's wrong here? I can't get my access levels working :( :oops:

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{

$query = "SELECT * FROM user WHERE username='$username'";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
          
            if ($row['access'] == 1);

header("Location: logout.php");

if ($row['access'] == 5) 
        
header("Location: index.php");
              
}

?>
I tried putting an else in there, but everything I do I get an error :(

Re: Access Levels

Posted: Fri Aug 30, 2013 3:01 pm
by Epiales
Have also tried this:

Code: Select all

<?php
session_start();
include("functions.php");
connect();

$query = "SELECT access from user WHERE id = '$id'";
       $result = mysql_query($query) or die(mysql_error());
       if(!$result) die ("Database access failed: " .mysql_error());
          $row = mysql_fetch_array($result); 

            if (mysql_num_rows($result) == 5) {
        
header("Location: index.php");

} else {
header("Location: logout.php");
              
}

?>

Re: Access Levels

Posted: Fri Aug 30, 2013 3:30 pm
by MikuzA
:)

mysql_num_rows in your case equals 1. due It's an function to retreive how many rows your query resulted.

Your first code looks more on the right track, I would adjust those if-sentences a bit.
You could do some debugging, like I bet you expect that the database returns a '5' in column access.

print it out to confirm you get this, then start adding the if's to validate what happens when access is something else.

Re: Access Levels

Posted: Fri Aug 30, 2013 3:55 pm
by Epiales
I'm not sure how to debug really :( :oops: :oops:

But I have this now. It really does nothing but say the page has a redirect and won't go anyway lol...

I know I'm close lol

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(isset($_SESSION['uid'])){


$query = "SELECT * FROM user WHERE id='$id'";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
          
            if ($row['access'] == 1)
{
header("Location: logout.php");

}else {  ($row['access'] == 5); 
        
header("Location: index.php");
              
}
}
?>

Re: Access Levels

Posted: Fri Aug 30, 2013 4:03 pm
by MikuzA
add a print $row['access']; or DIE($row['access']);
so that you can see does it have any value.
that's like trace-debugging.

Also, I would suggest doing the if-thingie reversed,

IF($access ==5) { admin awesomeness; }
elseif($access == 3) { gamemaster toolset; }
else { player access; }

Re: Access Levels

Posted: Fri Aug 30, 2013 4:09 pm
by Epiales
Okay, I put this in there:

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(isset($_SESSION['uid'])){


$query = "SELECT * FROM user ";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
        print $row['access'];  
            if ($row['access'] == 1)

?>
And it returned the value of 5, which is admin

Re: Access Levels

Posted: Fri Aug 30, 2013 4:12 pm
by hallsofvallhalla
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Code: Select all

if ($row['access'] == 1)
{
header("Location: logout.php");
exit;
}
if ($row['access'] == 5) 
{ 
header("Location: index.php");
exit;
}       

Re: Access Levels

Posted: Fri Aug 30, 2013 4:16 pm
by Epiales
hallsofvallhalla wrote:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Code: Select all

if ($row['access'] == 1)
{
header("Location: logout.php");
exit;
}
if ($row['access'] == 5) 
{ 
header("Location: index.php");
exit;
}       
I swear I've had it that way before. I put it and I just get the page has a redirect and won't access it either way.

Re: Access Levels

Posted: Fri Aug 30, 2013 4:18 pm
by hallsofvallhalla
wait, what do you get on the page?

Re: Access Levels

Posted: Fri Aug 30, 2013 4:21 pm
by hallsofvallhalla
are you getting the "this page has a redirect loop"?

It is most likely because your index page has a redirect on it as well. If this code is going in the index page then do not put the header redirect of index. Instead do this

Code: Select all

if ($row['access'] < 5)
{
header("Location: logout.php");
exit;
}

/// rest of page will run only if access level is 5.