0

first time here. I'm struggling to get this PHP function to work, and after 5 hours it's driving me insane.

I have this function:

// DOES THE USER HAVE ACCESS?
function access_la_page($id) {

include('DB_db.php');

/// GENERATE SQL 
$sql = "SELECT parent FROM la_pages WHERE id = $id";

// PREPARE THE STATEMENT
$result = mysqli_query($conn, $sql);

if($result === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
  echo "FAIL" . $conn->error; exit();
}

$row = mysqli_fetch_array($result);

// CHECK PERMISSIONS DATABASE TO SEE IF USER HAS ACCSS HIGHER UP THE CHAIN  
$sql2 = "SELECT * FROM la_pages_permissions WHERE la_page_id = $id AND user_id = " . $_SESSION['user_id'];

$result2 = mysqli_query($conn, $sql2);

if($result2 === false) {
  trigger_error('Wrong SQL: ' . $sql2 . ' Error: ' . $conn->error, E_USER_ERROR);
  echo "FAIL" . $conn->error; exit();
}

if(mysqli_num_rows($result2) > 0){

    $value = TRUE;

    return $value;

}elseif($row['parent']!==$id&&$row['parent']&&!isset($value)) { 
    access_la_page($row['parent']); // CHECK DB RECURSIVELY TO SEE IF USE HAS PERMISSION HIGHER UP THE CHAIN    
}


}

I then call this function:

if(access_la_page($_SESSION['la_page_id'])==TRUE){
    echo "Success";
}else{
    echo "Fail";
}

Now... If the function hits TRUE on the first instance it will in fact return TRUE to my page, success! But if it loops it does not return TRUE. It returns nothing.

But, when I echo from the statement I can see that the function is performing as it should, and when I echo $value, it shows TRUE and the function ceases - which is what should happen. But the function does not return TRUE.

Does that make sense, is this not working because I'm looping through the function?

EDIT - Excuse my ignorance. I had TRUE in quotes, I've modified that now but it doesn't work still

user3706091
  • 115
  • 3
  • 12
  • 2
    `"TRUE" !== TRUE`: one is a string, the other is a boolean – Mark Baker Jun 04 '14 at 08:08
  • @MarkBaker While that is correct, I am unsure it will make a difference, a non empty string will equal true when not using strict equality as he is not using: `echo TRUE == 'TRUE' ? 'OK' : 'FAIL'; // OK` – Flosculus Jun 04 '14 at 08:10
  • @Flosculus - It can make a lot of difference `echo FALSE == 'TRUE' ? 'OK' : 'FAIL';` – Mark Baker Jun 04 '14 at 08:11
  • @MarkBaker Granted, I've updated my answer to address a possible mishap in the functions return logic. – Flosculus Jun 04 '14 at 08:18

3 Answers3

0

Change

$value = "TRUE";

to

$value = TRUE;
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
0

Your final If statement will return 'TRUE' if it passes, but won't return anything if it does not.

Can you confirm this, as at the moment that function is returning either 'TRUE' or null. This might be the cause.

Try a return on the last clause, if the recursive check is meant to help decide, then it should return something:

if(mysqli_num_rows($result2) > 0){

    $value = "TRUE";

    return $value;

}elseif($row['parent']!==$id&&$row['parent']&&!isset($value)) {
    return access_la_page($row['parent']); // CHECK DB RECURSIVELY TO SEE IF USE HAS PERMISSION HIGHER UP THE CHAIN    
}
Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • THIS. This worked. Adding a simple return to the function call fixed everything. Thank you :) – user3706091 Jun 04 '14 at 08:19
  • You may want to use plain `true` instead of using a string containing the word as you cannot use strict equality as MarkBaker pointed out. – Flosculus Jun 04 '14 at 08:21
  • I did do that, in fact I did that first. but I was trying allsorts to try and figure out the issue. – user3706091 Jun 04 '14 at 08:30
  • May I ask why adding return to the access_la_page function call fixed this issue? – user3706091 Jun 04 '14 at 08:31
  • Because that function is recursive. Even if a function can return a value instantly, if it is capable of calling itself then it is recursive. Therefore you are returning the value of the call made by the first, second, or third... etc running. A recursive function usually returns either a value, or the value returned by calling itself. Its hard to explain, but if you look at recursive directory iteration (a manual implementation, not PHPs version) then you can see this in action. – Flosculus Jun 04 '14 at 10:53
0

Andy Gee's answer it correct, plus you can shorten your if statement condition to

if(!($result)){
      code here 

}

Yaje
  • 2,753
  • 18
  • 32