PHP conditions

Thread Solved

Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

PHP conditions

 
0
  #1
Dec 22nd, 2007
Below if part of my login script. I am trying to get it to redirect if the users account status is 'not verified' or 'suspended' (not verified is the field 'verified' = 0 and suspended is 'verified' = 2)

I'm sure it could be coded a little more eloquently, but this is what I have...anyone see why its not redirecting for non-verified/suspended users? (It does, however, redirect to the other pages(change.php and my.php) without a problem.)

  1. $sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
  2. $result=mysql_query($sql);
  3. $info = mysql_fetch_array($result, MYSQL_ASSOC);
  4. // Mysql_num_row is counting table row
  5. $count=mysql_num_rows($result);
  6. // If result matched $myusername and $mypassword, table row must be 1 row
  7.  
  8. if($count==1){
  9. if ($info['verified'] == 0) {
  10. $url = "notverified.php";
  11. $redirect = "other";
  12. }
  13. if ($info['verified'] == 2){
  14. $url = "suspended.php";
  15. $redirect = "other";
  16. }
  17. if ($myusername == "10001" && !($redirect == "other"))
  18. {$url = "change.php";
  19. }
  20. if (!($myusername == "10001") && !($redirect == "other"))
  21. {$url = "my.php";
  22. }
  23. session_register("myusername");
  24. session_register("mypassword");
  25. }else
  26. {$url = "login.php";}
  27. echo("<script>
  28. <!--
  29. location.replace(\"".$url."\");
  30. -->
  31. </script>");
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: PHP conditions

 
0
  #2
Dec 22nd, 2007
Try this:
  1. $sql="SELECT * FROM users WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
  2. $result=mysql_query($sql);
  3.  
  4.  
  5. $info = mysql_fetch_array($result, MYSQL_ASSOC);
  6.  
  7. // If result matched $myusername and $mypassword, table row must be 1 row
  8. if(count($info)==1){
  9. if ((int)$info['verified'] == 0) {
  10. $url = "notverified.php";
  11. $redirect = "other";
  12. }
  13. elseif ((int)$info['verified'] == 2){
  14. $url = "suspended.php";
  15. $redirect = "other";
  16. }
  17. elseif ($myusername == "10001" && !($redirect == "other"))
  18. {$url = "change.php";
  19. }
  20. elseif (!($myusername == "10001") && !($redirect == "other"))
  21. {$url = "my.php";
  22. }
  23. session_register("myusername");
  24. session_register("mypassword");
  25. }else
  26. {$url = "login.php";}
  27. echo("<script>
  28. <!--
  29. location.replace(\"".$url."\");
  30. -->
  31. </script>");
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

 
0
  #3
Dec 22nd, 2007
I appreciate the input. Thanks.

Tried it and its a no-go though(it just redirects EVERY user back to the login screen).

In the original code, the records are being retrieved from the database properly as it redirects to the other 2 pages fine(registered user page and the admin page) meaning it validates the login successfuly.

I assume the glitch is somewhere in the "IF" statements...multiple If/Else's give me a headache LOL
Last edited by verbob; Dec 22nd, 2007 at 3:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

 
0
  #4
Dec 22nd, 2007
Hmmm...perhaps its easier to just put a small script at the beginning on the redirected page(s) to check the status of the verified field, then redirect from there...just wanted to avoid an extra call to the DB(and an extra redirect).
Last edited by verbob; Dec 22nd, 2007 at 3:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

 
0
  #5
Dec 22nd, 2007
AHA!

This worked though:

  1. // username and password sent from signup form
  2. $myusername=$_POST['myusername'];
  3. $mypassword=$_POST['mypassword'];
  4.  
  5. $sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
  6. $result=mysql_query($sql);
  7. $count=mysql_num_rows($result);
  8. $redirect = "allowed";
  9. $info = mysql_fetch_array($result, MYSQL_ASSOC);
  10.  
  11. // If result matched $myusername and $mypassword, table row must be 1 row
  12. if($count==1){
  13. switch ($info['verified']){
  14. case 0:
  15. $url = "notverified.php";
  16. $redirect = "not_allowed";
  17. break;
  18. case 2:
  19. $url = "suspended.php";
  20. $redirect = "not_allowed";
  21. break;
  22. }
  23. if ($redirect == "allowed"){
  24. switch ($myusername){
  25. case '10001':
  26. $url = "change.php";
  27. break;
  28. default:
  29. $url = "my.php";
  30. break;
  31. }
  32. }
  33. session_register("myusername");
  34. session_register("mypassword");
  35. }
  36. if (!($count==1)){
  37. $url = "login.php";}
  38. echo("<script>
  39. <!--
  40. location.replace(\"".$url."\");
  41. -->
  42. </script>");
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP conditions

 
0
  #6
Dec 22nd, 2007
I realize this is marked as solved, but I'm guessing your problem with the original code comes from depending on values of undeclared variables.

(I.e., you need to define $redirect outside the scope of your IF statements. You seem to have accounted for this in your working code.)
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

 
0
  #7
Dec 22nd, 2007
That is quite possible. Thanks for the input

Although I thought PHP(atleast in newer versions) allowed variables to be defined 'on-the-fly'. (as an example - $url variable is not previously defined)..or perhaps its just tempermental lol
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP conditions

 
0
  #8
Dec 22nd, 2007
Originally Posted by verbob View Post
Although I thought PHP(atleast in newer versions) allowed variables to be defined 'on-the-fly'. (as an example - $url variable is not previously defined)..or perhaps its just tempermental lol
Yes, this is the case.

However, you still need to be sure of a variable's scope. Since you are setting the value of $redirect inside an IF statement scope (i.e., between the curly braces {}), the third and fourth IF statement conditions will fail, as they are not in the same scope where $redirect is given a value.

If you define $redirect before the start of your group of IF statements, they will share the same scope, and your code will pass.

From orig. code:
  1. if ($info['verified'] == 0) {
  2. $url = "notverified.php";
  3. $redirect = "other";
  4. }
  5. if ($info['verified'] == 2){
  6. $url = "suspended.php";
  7. $redirect = "other";
  8. }
  9. if ($myusername == "10001" && !($redirect == "other")){
  10. $url = "change.php"
  11. }
  12. if (!($myusername == "10001") && !($redirect == "other")){
  13. $url = "my.php";
  14. }
Note: if ($myusername == "10001" && !($redirect == "other")). Even though you are setting $redirect = "other" above, it is defined in a scope which is unaccessible at this point, and this line fails as a result. In other words, $redirect in this scope is undefined.

You can get PHP to tell you when you are using uninitialized variables.
http://ca3.php.net/error_reporting

Hope this clears it up.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: dil2011 is an unknown quantity at this point 
Solved Threads: 0
dil2011 dil2011 is offline Offline
Newbie Poster

Re: PHP conditions

 
0
  #9
Dec 26th, 2007
hi dont really knw anything abt comps but if u could help me in simple words plz
i triend logging into punjabijanta and i got this

Notice: Undefined variable: sourcedir in /home/.tinkertoy/jatt4chat/punjabijanta.com/index.php on line 49

Warning: require_once(/QueryString.php) [function.require-once]: failed to open stream: No such file or directory in /home/.tinkertoy/jatt4chat/punjabijanta.com/index.php on line 49

Fatal error: require_once() [function.require]: Failed opening required '/QueryString.php' (include_path='.:/usr/local/php5/lib/php:/usr/local/lib/php') in /home/.tinkertoy/jatt4chat/punjabijanta.com/index.php on line 49


plz help
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 45
Reputation: verbob is an unknown quantity at this point 
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

 
0
  #10
Dec 26th, 2007
Thanks Bill Mac. Actually, it does clear it up.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC