NoDoze Posted August 12, 2008 Share Posted August 12, 2008 I have this code: <?php session_start(); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'config.php'; include 'opendb.php'; $userId = $_POST['txtUserId']; $password = $_POST['txtPassword']; $sql = "SELECT user_id FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; header('Location: balance/index.php'); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } include 'closedb.php'; } ?> Used for authenticating users in a mysql database, then redirecting them to a web page. However I want to tweak this, so that based on the user it redirects to their personal web page. I tried modifying this line: if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; header('Location: balance/index.php'); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } To: if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true;} ifelse ($_POST['txtUserId'] == "balance") { header("Location: balance.php"); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } But I can't get the syntax correct... AND... this should also allow me to add more users, correct? So it could look like this: if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true;} ifelse ($_POST['txtUserId'] == "balance") { header("Location: balance.php"); ifelse ($_POST['txtUserId'] == "triangle") { header("Location: triangle.php"); ifelse ($_POST['txtUserId'] == "square") { header("Location: square.php"); ifelse ($_POST['txtUserId'] == "circle") { header("Location: circle.php"); exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } Am I going about this the correct way, or am I way off...? Thanks! Quote Link to comment Share on other sites More sharing options...
mrhobbz Posted August 12, 2008 Share Posted August 12, 2008 Else If statement, refer to: http://us.php.net/elseif Quote Link to comment Share on other sites More sharing options...
trq Posted August 12, 2008 Share Posted August 12, 2008 There is no such thing in php as ifelse, not sure where your going with that. This... if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; if (file_exists($_POST['txtUserId']} . ".php")) { header("Location: {$_POST['txtUserId']}.php"); } else { header('Location: balance/index.php'); } exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } should help you out though. Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true;} switch ( $_POST['txtUserId'] ) { case 'balance': $page = 'balance'; case 'triangle': $page = 'triangle'; case 'square': $page = 'square'; case 'circle': $page = 'circle'; default: $page = 'error'; } header("Location: $page.php"); exit; } else {... Quote Link to comment Share on other sites More sharing options...
mrhobbz Posted August 12, 2008 Share Posted August 12, 2008 I would go with a switch/case like discomat mentioned. Probably the cleanest way to do it. Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 12, 2008 Author Share Posted August 12, 2008 Opps... typo, I meant: elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true I'll give that a try thanks! Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 12, 2008 Author Share Posted August 12, 2008 Wow....gots lots of responses...Cool! I prefer Thorpes response best cause it's dynamic and wouldn't require me to add code for every new user... I'll give it a try, thanks! Quote Link to comment Share on other sites More sharing options...
mrhobbz Posted August 12, 2008 Share Posted August 12, 2008 Wow....gots lots of responses...Cool! I prefer Thorpes response best cause it's dynamic and wouldn't require me to add code for every new user... I'll give it a try, thanks! Didn't even see Thorpes response up there, yeah thats a pretty nice setup as well. Either one of the three should do you well. Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 12, 2008 Author Share Posted August 12, 2008 Woops.....I think there is a syntax error somewhere.... I only get a blank white page... <?php session_start(); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'config.php'; include 'opendb.php'; $userId = $_POST['txtUserId']; $password = $_POST['txtPassword']; $sql = "SELECT user_id FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; if (file_exists($_POST['txtUserId']} . ".php")) { header("Location: {$_POST['txtUserId']}.php"); } else { header('Location: login.php'); } exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } include 'closedb.php'; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 12, 2008 Share Posted August 12, 2008 Yeah, this... if (file_exists($_POST['txtUserId']} . ".php")) { ought be.... if (file_exists($_POST['txtUserId'] . ".php")) { Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 12, 2008 Author Share Posted August 12, 2008 wow, yea, I was playing with the exact same line, was wondering why there was a } in there! Got it all working Loved the response times!! THANKS! Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 ok...something went wrong... I cleared out the users in the mysql database, and started creating new users... I also tweaked the php again to redirect to a subfolder of the user... And now only one user can login...? <?php session_start(); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { include 'config.php'; include 'opendb.php'; $userId = $_POST['txtUserId']; $password = $_POST['txtPassword']; $sql = "SELECT user_id FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password')"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; if (file_exists($_POST['txtUserId'] . ".php")) { header("Location: {$_POST['txtUserId']}/index.php"); } else { header('Location: logout.php'); } exit; } else { $errorMessage = 'Sorry, wrong user id / password'; } include 'closedb.php'; } ?> What did I do wrong? I get the feeling like the query is only finding the first user and not searching the database for valid users.... is my guess correct? What would I do to tweak this? Thanks! Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 ok, I narrowed it down.... It doesn't like the subfolder thingy... header("Location: {$_POST['txtUserId']}/index.php"); It works ok, if it's to a file in the root only. How could I change that so it's a subfolder of the username? Thanks! Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 ok, I don't get it.... header("Location: {$_POST['txtUserId']}/{$_POST['txtUserId']}.php"); works fine! So why would: header("Location: {$_POST['txtUserId']}/index.php"); not work? Thanks! Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 ok something wierd is going on with this code... In the root, where the login is located, I have these files: closedb.php config.php opendb.php logout.php login.php user.php When the code is set to: header("Location: {$_POST['txtUserId']}.php"); The redirect goes to the correct user.php file at the root. When I move the user.php file into a sub folder of the user name, so there is no user.php file at the root, and change the code to: header("Location: {$_POST['txtUserId']}/{$_POST['txtUserId']}.php"); It failes to redirect, just reverting back to the login page. HOWEVER! When I move the user.php file into a sub folder of the user name, so there IS STILL user.php file at the root, and have the code to: header("Location: {$_POST['txtUserId']}/{$_POST['txtUserId']}.php"); It redirects to the user.php in the sub folder ok! The URL being: http://domain.com/files/user/user.php Why would it need to have both the user.php in the root AND the sub folder in order to redirect to the subfolder user.php file? I am seriously stumped... Thanks. Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 OMG, I think it has to do with the line above it...? if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; if (file_exists($_POST['txtUserId'] . ".php")) { header("Location: {$_POST['txtUserId']}/{$_POST['txtUserId']}.php"); } else { header('Location: http://www.balancehydro.com/files/logout.php'); if (file_exists($_POST['txtUserId'] . ".php")) ...is checking for the file, huh? How would I tweak this??? thanks. Quote Link to comment Share on other sites More sharing options...
NoDoze Posted August 13, 2008 Author Share Posted August 13, 2008 Well, over some lamenting...and lunch ... I got it working... if (file_exists($_POST['txtUserId'] . "/index.php")) { header("Location: {$_POST['txtUserId']}/index.php"); Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.