1

I have asked this before but code still not working.

I have a login system which works fine but the next step I want to do is redirect each user depending on their medals they have, which is either 1, 2, 3, 4 or 5. In the mysql database I have a field called medals which is pre filled in for each username and password to 1 of the 5 amounts.

so lets say a user with 1 medal logs in i want it to go to location 'page1.html'

if a user with 2 medals logs in i want it to go to location 'page2.html'

if a user with 3 medals logs in i want it to go to location 'page3.html' and so on.

I have tried the following code but it does not redirect successfully:

    // Check username and password match
    if (mysql_num_rows($login) == 1) {
            // Set username session variable
            $_SESSION['username'] = $_POST['username'];
            // Jump to secured page
            $row = mysql_fetch_row($sql);
    switch ($row['medals']):
        case 1:
            header('location: page1.html');
            exit;
        case 2:
            header('location: page2.html');
            exit;
         case 3:
            header('location: page3.html');
            exit;
         case 4:
            header('location: page4.html');
            exit;
         case 5:
            header('location: page5.html');
            exit;
}
else {
        // Jump to login page
        header('Location: login.php');
}

?>

any help would be great

user1188711
  • 49
  • 1
  • 8
  • What happens instead of successful redirection? Are you sure `$row['medals']` is 1-5 and contains what you think? – drew010 Feb 04 '12 at 01:46
  • hi yes for example i am entering username and password for a person that has a default value of 2 'medals' in the 'medals' field in mysql so it should go to page2.html, instead it just goes onto the page above (loginupdate.php) and its blank no data. so its like it logs in successfully but it cannot carry out the functions so from $row = mysql_fetch_row($sql); onwards there is an error somewhere – user1188711 Feb 04 '12 at 01:58

2 Answers2

0

You need to change $row = mysql_fetch_row($sql); to $row = mysql_fetch_row($login);

mysql_fetch_row() takes the resource that was returned from mysql_query() rather than the SQL query string (the same variable you passed to mysql_num_rows().

See if that helps any. You may also want to have a default case that redirects to a homepage or something in case an unexpected value gets in the medals column.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • thanks for your help. i tried both still to no avail. The medals value is stored in the database. I just want to compare this value and if its 1,2,3,4 or 5 it will go to 1 of the 5 links above depending on value in mysql. Can you conform the code should work? – user1188711 Feb 04 '12 at 02:27
  • I am logging in a on a specific username and password stored on the database. This username & pass have 'medals' feild set as '2'. I have it stored as an INT and inserted a value of 2 in 'value' column. Is this how it should be stored in mysql for this type 'fetching' thanks – user1188711 Feb 04 '12 at 02:35
  • From the few lines I can see it seems like it should. Try adding `var_dump($row)` after you call mysql_fetch_row and see what you get. – drew010 Feb 04 '12 at 02:35
  • no joy, it seems to be going to the page the code is on no matter if the pass is right or wrong, when i remove all of the above code and just put 'Location: *****.php' it works great but I need it to redirect depending on how many medals they have. have you got msn? – user1188711 Feb 04 '12 at 02:49
  • i would like to get the code to you. are the { } placed correctly? – user1188711 Feb 04 '12 at 03:33
  • Did you confirm the contents of `$row` using var_dump? – drew010 Feb 04 '12 at 03:42
  • yes i tried: $row = mysql_fetch_row($sql); (NEW LINE) var_dump($row); (NEW LINE) switch ($row['medals']): – user1188711 Feb 04 '12 at 14:09
  • And what did var dump say? NULL? I still think `mysql_fetch_row($sql)` should be `mysql_fetch_row($login)` based on the little bit of code. Or mysql_fetch_row($login) is wrong. – drew010 Feb 04 '12 at 19:48
  • array(13) { [0]=> string(5) "hobbs" [1]=> string(1) "2" [2]=> string(11) "luke Hobbs" [3]=> string(12) "joe Hobbs" [4]=> string(0) "" [5]=> string(0) "" [6]=> string(0) "" [7]=> string(32) "6e9a9f428269a03d356a59e6885c0240" [8]=> string(0) "" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" [12]=> string(0) "" } – user1188711 Feb 04 '12 at 20:57
  • There is the problem, no `'medals'` value in there. Try using `mysql_fetch_array()` instead of `mysql_fetch_row`. – drew010 Feb 04 '12 at 21:04
  • genius its working now. thanks very much. would you know the code to logout automatically after 3600 seconds? also when the user logs in they choose a colour from a drop down (created via form not stored in mysql) and when they submit it saves it to the database. This is all working if the user logs back in again it displays 'please select'. Is there any possibility I can display what they have inserted previously? – user1188711 Feb 04 '12 at 21:31
  • http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes And yes, you can display what the previously selected. When you generate the list box, you need to look at the previously stored value from the database and if it matches the current item, add `selected="selected"` to the list box item and it will default to their previous value. – drew010 Feb 04 '12 at 21:41
  • would this code still work for the login page: // Check, if user is already login, then jump to secured page if (isset($_SESSION['username'])) { $row = mysql_fetch_array($login); switch ($row['medals']): case 1: header('Location: page1.html'); exit; case 2: header('Location: securedpage.php'); exit; – user1188711 Feb 04 '12 at 22:25
  • Yeah I think so, try it and see. – drew010 Feb 04 '12 at 22:32
0

Check this it may be useful

$listQry = 'SELECT * FROM medals';
$listRes = mysql_query($listQry);
while($listRow = mysql_fetch_row($listRes)):
        $data[] = $listRow[0];
endwhile;


foreach($data as $k=>$v):

    switch($data[$k]):
        case 1 : header('Location: page1.html');exit;break;
        case 2 : header('Location: page2.html');exit;break;
    endswitch;

endforeach;
Sam Arul Raj T
  • 1,752
  • 17
  • 23