0

I'm trying this code to check if a user is verified and redirecting them if they are not. I'm having trouble with the result as it keeps redirecting me regardless of if I am verified or not. Here is the code:

<?php
    if(loggedin()){ 
    $check_active_sql = mysql_query("SELECT * FROM members WHERE username = '$username'") or die(mysql_error());
    while ($check_active_res = mysql_fetch_array($check_active_sql)) {
        $active = $check_active_res['active'];
            if($active == "0"){
                header("Location: login.php?verify=true");
            }
            else
            {
            }
        }
    };
?>

Thank you

  • 3
    It's difficult to know where the problem is. Please follow the regular debugging process by [`var_dump`ing](http://www.php.net/manual/en/function.var-dump.php) your variables at different points and provide us with the results. You'll either solve the problem yourself, or you'll give us more information to help you. – Adi Mar 16 '13 at 09:27
  • @user2152265 I have updated my answer please check it out – sandip Mar 16 '13 at 09:29

2 Answers2

1

I'm assuming that once a user logs in you set a field in the database to 1 Why not use sessions instead of using this way?

Simply as soon as you login a user put the following code

session_start();
$_SESSION['logged_in'] = 1;

Now, to check if a user is logged in, simply add this function

function check_login() {
if($_SESSION['logged_in'] == 1){
return true;
} else {
return false;
}

then if you want to redirect him simply do the following

if(!check_login()) {
header('location: http://website.com');
}
Ali
  • 3,479
  • 4
  • 16
  • 31
  • Sorry, in this case $active relates to weather the member has validated their account via email so a session wouldn't work in this case - However I will definitely use this code example if I decide to log members who are online, thanks. Do you know of any ways of ensuring that a member definately validates their account? – user2152265 Mar 16 '13 at 14:50
-1

try by using empty() as:

     if(empty($active) && $active!=1){
         header("Location: login.php?verify=true");
     }else{

     }

instead of

     if($active == "0"){}

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

sandip
  • 3,279
  • 5
  • 31
  • 54
  • Would please explain why? – Adi Mar 16 '13 at 09:23
  • No that doesn't work either – user2152265 Mar 16 '13 at 09:25
  • While your explanation is valid to document the behaviour of [`empty()`](http://php.net/manual/en/function.empty.php), you give no clue to the OP as why would this solve his problem – Adi Mar 16 '13 at 09:29
  • It's ok, I understand how that would work but my database actually has the number 0 in the database by default and changes to 1 when verified - I guess I'd have to get rid of the 0 value – user2152265 Mar 16 '13 at 09:35
  • @user2152265 gr8! you understood this, as you said you have values 0 & 1 (if confirmed) the above function will do the job as it checks for (string, false, array(), NULL, “0?, 0, and an unset variable) , you can also modify more precisely as if(empty($active) && $active!=1){} – sandip Mar 16 '13 at 09:41
  • 1
    `empty` is the same as `== false`. Don't use `empty` *unless you don't know whether the variable you're testing exists or not*. – deceze Mar 16 '13 at 09:41
  • Tried the answer that you gave but for some reason now it logs me in if I'm not verified (0) and redirects me if I am (1) – user2152265 Mar 16 '13 at 09:45
  • Ok, I just noticed the ! so I removed that and it works however I now get an error on the header ( Cannot modify header information )? – user2152265 Mar 16 '13 at 09:48
  • @user2152265 I think that is a different problem , for more about this refer this http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – sandip Mar 16 '13 at 09:54
  • @user2152265 Your welcome! , I am happy that my answer helped you – sandip Mar 16 '13 at 10:18