PHP user auth help...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lb_mike
    New Member
    • May 2006
    • 3

    #1

    PHP user auth help...

    greetings -

    I am very much new the PHP world and have what I believe to be a simple question..

    I am using one of the Dreamweaver User Auth scripts with a mySQL database. I have the verification process down, but I would like to redirect each user to a different directory/page.

    In the mySQL I have the data for where I would like to redirect each user, but I can't get it to translate properly.

    Basically, I want a user to be able to login from a central page and be taken to their site/page automatically.. .

    Here's the code.... The line in bold is where I believe I am doing something wrong. Please let me know if you have anythoughts!! Thanks!!


    <?php require_once('. ./Connections/database.php'); ?>
    <?php
    // *** Validate request to login to this site.
    if (!isset($_SESSI ON)) {
    session_start() ;
    }

    $loginFormActio n = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
    $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    }

    if (isset($_POST['user'])) {
    $loginUsername= $_POST['user'];
    $password=$_POS T['pass'];
    $MM_fldUserAuth orization = "";
    $MM_redirectLog inSuccess = "/" & $_POST['redirect'] & "/index.html";
    $MM_redirectLog inFailed = "http://clients.mysite. com";
    $MM_redirecttoR eferrer = false;
    mysql_select_db ($database_lp_d atabase, $lp_database);

    $LoginRS__query =sprintf("SELEC T user, pass FROM authentication WHERE user='%s' AND pass='%s'",
    get_magic_quote s_gpc() ? $loginUsername : addslashes($log inUsername), get_magic_quote s_gpc() ? $password : addslashes($pas sword));

    $LoginRS = mysql_query($Lo ginRS__query, $lp_database) or die(mysql_error ());
    $loginFoundUser = mysql_num_rows( $LoginRS);
    if ($loginFoundUse r) {
    $loginStrGroup = "";

    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;

    if (isset($_SESSIO N['PrevUrl']) && false) {
    $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
    }
    header("Locatio n: " . $MM_redirectLog inSuccess );
    }
    else {
    header("Locatio n: ". $MM_redirectLog inFailed );
    }
    }
    ?>

    Thanks in advance for any advice!
  • Vagabond
    New Member
    • Apr 2006
    • 17

    #2
    I'm learning PHP myself but I think I see the problem. I believe this line is wrong.

    [PHP]$MM_redirectLog inSuccess = "/" & $_POST['redirect'] & "/index.html";[/PHP]

    Try changing it to this:

    [PHP]$MM_redirectLog inSuccess = "/".$_POST['redirect']."/index.html";[/PHP]

    or try this:

    [PHP]$MM_redirectLog inSuccess = "../" . $_POST['redirect'] . "/index.html";[/PHP]
    Last edited by Vagabond; May 18 '06, 05:35 PM.

    Comment

    • lb_mike
      New Member
      • May 2006
      • 3

      #3
      Thanks for that... at least now I don't have an error message. for some reason I am getting redirected to www.mysite.com//index.html (notice the 2 /'s) which makes be believe that the "." are the right way to go, but the function to call up the info from the database isn't working, etc.

      any other thoughts?

      -m

      Comment

      • Vagabond
        New Member
        • Apr 2006
        • 17

        #4
        Originally posted by lb_mike
        Thanks for that... at least now I don't have an error message. for some reason I am getting redirected to www.mysite.com//index.html (notice the 2 /'s) which makes be believe that the "." are the right way to go, but the function to call up the info from the database isn't working, etc.

        any other thoughts?

        -m
        Are the users filling out a form with the $_POST['redirect'] code ?
        I don't see this part being pulled from a database. You are trying to pull it from a form. If it's not being filled out in a form then that would be why the redirect information is missing or mispelled on the form perhaps.

        [PHP]mysql_select_db ($database_lp_d atabase, $lp_database);[/PHP]

        I also don't see where $database_lp_da tabase is defined as a variable in this script. Is it a global variable in: <?php require_once('. ./Connections/database.php'); ?> some place ? If so you need to define the global variable before the mysql_select_db statement.

        [PHP]global $database_lp_da tabase;[/PHP]

        Comment

        • lb_mike
          New Member
          • May 2006
          • 3

          #5
          good call on defining the database - caught that after I posted this.. but thanks!!

          as for the redirect being pulled from a form, that is not what I was looking for.. .basically, if the user and password match, I want it to redirect them to a specifc directory - the name of which is already stored in the database.

          Example

          username | password | redirect |
          -----------|---------------|-------------|
          user1 | pass1 | user1dir |

          so for user one, the would get redirected to www.mysite.com/user1dir


          I think that I just need to make this..
          $MM_redirectLog inSuccess = "../" . $_POST['redirect'] . "/index.html";

          to pull from the database and not from a form. Still working on that part.. any ideas

          thanks for your help so far!!! I really appreciate it!

          Comment

          • Vagabond
            New Member
            • Apr 2006
            • 17

            #6
            I'm not real good at SQL yet but it would seem to me that you MIGHT (Not sure) need to change this line below to add in the user directory to your query. However I think you are still getting the whole table and putting it into $LoginRS

            Code:
            $LoginRS__query=sprintf("SELECT user, pass FROM authentication WHERE user='%s' AND pass='%s'",
            get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
            Then after you get your results in the line below.

            Code:
            $LoginRS = mysql_query($LoginRS__query, $lp_database) or die(mysql_error());
            You will then need to move this line below the line above and fill in the entry from the query for the redirect.

            Code:
            $MM_redirectLoginSuccess = "../" . $_POST['redirect'] . "/index.html";
            That should give you the result your looking for. As for the exact query to make I'm just not sure I understand SQL well enough to attempt that one since I don't know what all the functions your calling are. As I said before you might already have read the entire table into $LoginRS so you could try moving the line and adding this line instead.

            Code:
            $MM_redirectLoginSuccess = "../" . $LoginRS['redirect'] . "/index.html";

            Comment

            • Vagabond
              New Member
              • Apr 2006
              • 17

              #7
              After looking through the code again I think your query is fine as it is and you just need to move the one line and rewrite it to pull the data from $LoginRS like I said.

              It seems to just be a simpla matter of moving the one line and getting the table information. You might also need to add another line similar to this but I'm not sure if it's needed or not.

              $newArray = mysql_fetch_arr ay($LoginRS);

              Then change to this.

              $MM_redirectLog inSuccess = "../" . $newArray['redirect'] . "/index.html";

              That would make it look something like the code below. However I am also including a login sql/php script snipit that I used before to give you a more basic structure you can follow. You will notice I have left out a couple things from the code I use and I'm not sure if your code needs them or not.

              I am talking about this section here.

              if(mysql_num_ro ws($result) == 1){

              while ($newArray = mysql_fetch_arr ay($result)){
              $userid = $newArray['userid'];
              $name = $newArray['name'];
              $password = $newArray['password'];
              $security = $newArray['security'];
              $email = $newArray['email'];
              }
              mysql_close();
              Display_Main_Me nu();
              } else {


              Your code should look something like what is below or add in the section I left out.

              Code:
              $LoginRS__query=sprintf("SELECT user, pass FROM authentication WHERE user='%s' AND pass='%s'",
              get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
              
              $LoginRS = mysql_query($LoginRS__query, $lp_database) or die(mysql_error());
              $loginFoundUser = mysql_num_rows($LoginRS);
              $newArray = mysql_fetch_array($LoginRS);
              $MM_redirectLoginSuccess = "../" . $newArray['redirect'] . "/index.html";
              Here is a little code snipit that I use for one of my php programs that might help you figure out the correct code syntax for the sql statement. It's not as complex as yours but should give you an idea of where to go if the above don't work. I'm kinda new to SQL and PHP so you might need to try and follow the format below that I know works and modify your code to fit it.

              Code:
                $sql = "SELECT * FROM user where name = '$_POST['name']' AND password = password('$_POST['password']')";
                $result = mysql_query($sql, $conn) or die(mysql_error());
              
                if(mysql_num_rows($result) == 1){
              
                    while ($newArray = mysql_fetch_array($result)){
                      $userid = $newArray['userid'];
                      $name = $newArray['name'];
                      $password = $newArray['password'];
                      $security = $newArray['security'];
                      $email = $newArray['email']; 
                    }
                mysql_close();
                Display_Main_Menu();
                } else {
                  // Display the login page.
                  mysql_close();
                  Login_Menu();
                }
              Sorry if I am confusing you but I am a little lost on this part myself. I do think you will need the part of the code I skipped over but I'm just not sure.
              Last edited by Vagabond; May 24 '06, 07:39 AM.

              Comment

              Working...