User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 428,634 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1346 | Replies: 9 | Solved
Reply
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

PHP conditions

  #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>");
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: PHP conditions

  #2  
Dec 22nd, 2007
Try this:
$sql="SELECT * FROM users WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
$result=mysql_query($sql);


$info = mysql_fetch_array($result, MYSQL_ASSOC);

// If result matched $myusername and $mypassword, table row must be 1 row
if(count($info)==1){
	if ((int)$info['verified'] == 0) {
		$url = "notverified.php";
		$redirect = "other";
		} 
	elseif ((int)$info['verified'] == 2){
		$url = "suspended.php";
		$redirect = "other";
		}
	elseif ($myusername == "10001" && !($redirect == "other"))
		{$url = "change.php";
		}
	elseif (!($myusername == "10001") && !($redirect == "other"))
		{$url = "my.php";
		}
	session_register("myusername");
	session_register("mypassword"); 
}else
	{$url = "login.php";}
echo("<script>
	<!--
	location.replace(\"".$url."\");
	-->
	</script>");
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

  #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 2:36 pm.
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

  #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 2:52 pm.
Reply With Quote  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

  #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>");
  43.  
Reply With Quote  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP conditions

  #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  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

  #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  
Join Date: Dec 2007
Posts: 16
Reputation: bill mac is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bill mac bill mac is offline Offline
Newbie Poster

Re: PHP conditions

  #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  
Join Date: Dec 2007
Posts: 1
Reputation: dil2011 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dil2011 dil2011 is offline Offline
Newbie Poster

Re: PHP conditions

  #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  
Join Date: Sep 2007
Posts: 31
Reputation: verbob is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
verbob verbob is offline Offline
Light Poster

Re: PHP conditions

  #10  
Dec 26th, 2007
Thanks Bill Mac. Actually, it does clear it up.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC