| | |
PHP conditions
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 45
Reputation:
Solved Threads: 1
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.)
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.)
php Syntax (Toggle Plain Text)
$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $info = mysql_fetch_array($result, MYSQL_ASSOC); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ if ($info['verified'] == 0) { $url = "notverified.php"; $redirect = "other"; } if ($info['verified'] == 2){ $url = "suspended.php"; $redirect = "other"; } if ($myusername == "10001" && !($redirect == "other")) {$url = "change.php"; } if (!($myusername == "10001") && !($redirect == "other")) {$url = "my.php"; } session_register("myusername"); session_register("mypassword"); }else {$url = "login.php";} echo("<script> <!-- location.replace(\"".$url."\"); --> </script>");
•
•
Join Date: Dec 2007
Posts: 113
Reputation:
Solved Threads: 17
Try this:
PHP Syntax (Toggle Plain Text)
$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>");
•
•
Join Date: Sep 2007
Posts: 45
Reputation:
Solved Threads: 1
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
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.
•
•
Join Date: Sep 2007
Posts: 45
Reputation:
Solved Threads: 1
AHA!
This worked though:
This worked though:
php Syntax (Toggle Plain Text)
// username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $count=mysql_num_rows($result); $redirect = "allowed"; $info = mysql_fetch_array($result, MYSQL_ASSOC); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ switch ($info['verified']){ case 0: $url = "notverified.php"; $redirect = "not_allowed"; break; case 2: $url = "suspended.php"; $redirect = "not_allowed"; break; } if ($redirect == "allowed"){ switch ($myusername){ case '10001': $url = "change.php"; break; default: $url = "my.php"; break; } } session_register("myusername"); session_register("mypassword"); } if (!($count==1)){ $url = "login.php";} echo("<script> <!-- location.replace(\"".$url."\"); --> </script>");
•
•
Join Date: Dec 2007
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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
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:
php Syntax (Toggle Plain Text)
if ($info['verified'] == 0) { $url = "notverified.php"; $redirect = "other"; } if ($info['verified'] == 2){ $url = "suspended.php"; $redirect = "other"; } if ($myusername == "10001" && !($redirect == "other")){ $url = "change.php" } if (!($myusername == "10001") && !($redirect == "other")){ $url = "my.php"; }
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.
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- PHP Warning: Cannot modify header information - headers already sent by (output ..... (PHP)
- can not browse using url's only ip's work (Viruses, Spyware and other Nasties)
- IF Conditions (PHP)
- Php Syntax Error (PHP)
- Concept to fruition - the chronicle of a new site (Growing an Online Community)
- Greeting Your Members & Guests (PHP)
- Need help with craps program (C++)
- Help: Outputting Up and down arrows in C++ (C++)
- RewriteCond and RewriteRule (Linux Servers and Apache)
Other Threads in the PHP Forum
- Previous Thread: formatting textarea text
- Next Thread: Help
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube






