Jump to content

PHP & MySQL User Login Tweak....


NoDoze

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 {...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
}
?>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.