Web Analytics Made Easy -
StatCounter Loop help in password script - CodingForum

Announcement

Collapse
No announcement yet.

Loop help in password script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop help in password script

    I have a simple password script in which the username/password info is displayed as follows:
    PHP Code:
    if($name == $un1 && $pass == $pw1){
        
    Header ("Location: page1.html");
        exit;
    }
    if(
    $name == $un2 && $pass == $pw2){
        
    Header ("Location: page2.html");
        exit;

    ....and so on. I am trying to create a loop that will automatically generate that code for the number of users I require (i.e. if $ClientNumber = 10, then that function would be printed 10 times. Also, I need the number in the variable $un and $pw to be increased each time, as well (so I can control them with included individual variables).

    This is what I have so far, but now I am stuck. I may be totally off because I am very new at this....

    PHP Code:
    $ClientNumber 10;
    while (
    $ClientNumber 0)

    print ( 
    if(
    $name == $un1 && $pass == $pw1){
        
    Header ("Location: page1.html");
        exit;
    }
     ); 
    $ClientNumber--; 

    Any help is really appreciated! I am trying to learn PHP and any hints will be great!

    thanks!

  • #2
    well while this should work ...
    PHP Code:
    <?php
    for( $x=1;$x<=10;++$x ){
        if( 
    $name == ${'un'.$x} && $pass == ${'pw'.$x} ){
            
    Header ("Location: page$x.html");
            exit;
        }
    }
    ?>
    ...methinks you are doing something strange to have to do this in the first place?
    There is almost certainly a better way.
    Perhaps explain where your usernames and passwords are coming from in the first place to end up with this situation.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

    Comment


    • #3
      Thank you for your reply. To explain better, the login script should work fine as it was originally. The usernames and passwords are submitted through a form ($name and $pass), which are matched up to the correct name/pass with the ==. However, this means that to add a new user, I have to go into the code and duplicate the function:
      PHP Code:
      if($name == "JaneDoe" && $pass == "mypassowrd"){
          
      Header ("Location: page1.htm");
          exit;

      every time, and manually change the names and locations, etc.

      What I was hoping to do was have a seperate include page that had:

      - A variable such as $ClientNumber by which I could specify the number of user/pass functions that would be printed

      - variables such as $un1/$pw1, $un2/$pw2 and so on to replace the "JaneDoe" and "mypassword" that I could also edit to assign each new client a name and password

      For example, if in the seperate include page I had a list of variables that looked like this....
      PHP Code:
      $ClientNumber 2;

      $un1 "JaneDoe"$pw1 "mypassword"$loc1 "page1.htm";

      $un2 "JohnSmith"$pw1 "iloveyou"$loc2 "page2.htm"
      ....the main script would print the user/pass function 2 times, and automatically adjust the numbers of the variables in each function to correspond with the variables of the above include, maybe looking something like this:
      PHP Code:
      for($x=1;$x<=$ClientNumber;++$x){
          if( 
      $name == ${'un'.$x} && $pass == ${'pw'.$x} ){
              
      Header ("Location: ${'loc'.$x}");
              exit;
          }

      Sadly, the above doesn't work.

      I hope this better explains what I am trying to do. I realize that working the original script is fairly easy, and I am not so lazy as to protest to it as is, but I do have two reasons for trying to get this to work:

      - Size: what happens when user numbers get up to 20 or 30. Then management difficulty and script size will increase significantly.

      - Learning: this is all a learning process for me. While I very well could just find a premade script and use it no questions asked, I wouldn't learn a thing. This way, with the help of people like yourself, I am able to learn more and more about PHP.

      Thanks for your help so far!
      Last edited by mtd; Feb 7, 2004, 02:05 PM.

      Comment


      • #4
        So you're planning on having a completely different page for each user?


        herm....

        Where are you storing the usernames/passwords? MySQL would be a great place to keep that info, then something like:

        PHP Code:
        ...
        if (
        mysql_num_rows(mysql_query("SELECT client_number FROM tbl_users WHERE username = '$un' AND password = '$pw'")) != 0) {
           
        $page $un ".html";
           
        header ("Location: $page);
        } else {
           print "
        Invalid username and/or passwordPlease try again.";
        }

        ... 
        would work much better, I would think....

        Comment


        • #5
          Thanks Celtboy. However, mySQL is a whole new world for me! I am still barely learning PHP, and haven't begun to set foot into the realm of SQL. Maybe I will look into a good tutorial or something, as I am convinced it is a wonderful tool.

          Thanks guys for your help.

          Comment


          • #6
            MTD, I think you'll quickly find mysql to be invaluable to your web-based projects.

            I've whipped up the following code to possibly help you out.

            PHP Code:
            <?php
            /* Assume the following:
               1 - This page is called 'index.php'
               2 - This page was reached via a url like so:
               [url]http://www.mysite.com/index.php?user=[/url]<USERNAME>&password=<PASSWORD>
               
               Purpose:
                  This script will redirect a user with proper credentials
                  to a page 
            */


            $users = array(
               
            "bobdole" => array (
                  
            "user_num"  => 1,
                  
            "full_name" => "Bob Dole",
                  
            "email"     => "[email protected]",
                  
            "password"  => "bobpassword",
                  
            "page"      => "bob_random.htm"
               
            ),
               
            "janedoe" => array (
                  
            "user_num"  => 2,
                  
            "full_name" => "Jane Doe",
                  
            "email"     => "[email protected]",
                  
            "password"  => "janepassword",
                  
            "page"      => "index.php"
               
            ),
               
            "johndoe" => array (
                  
            "user_num"  => 3,
                  
            "full_name" => "John Doe",
                  
            "email"     => "[email protected]",
                  
            "password"  => "johnpassword",
                  
            "page"      => "johnwho.htm"
               
            ),
            );

            if (isset(
            $_GET["user"])) {
               
            $user $_GET["user"];
            } else {
               print 
            "No username was entered! Try again!";
               exit();
            }
                
                
                
            if (isset(
            $users[$user])) {
               
            $user_array $users[$user];
               
            $valid_password $user_array["password"];
               
               if (
            $_GET["password"] == $valid_password) {
                  
                  
            /****************** THE LOCATION SECTION *********************/
                  
            $location "Location: " $user_array["page"];
                  
            //$location = "Location: page" . $user_array["user_num"] . ".htm";
                  //$location = "Location: " . $user . ".htm");
                  //$location = "Location: " . $user . "-" . $user_array["password"] . ".htm";
                  
                  
                  
            header ($location);
               } else {
                  print 
            "Invalid Password.";
               }
            } else {
               print 
            "That user does not exist.";
            }


            ?>
            The code is simple:

            Users / user info is stored in associative arrays. The way it works-
            It checks the URL made for the request. If there is a valid username in the url, it compares passwords, otherwise gives an error.

            At the present, upon success, the user is redirected to a page stored in the value of

            "page"

            for that user, in their array. There are several opportunities for customization here.

            1- Depending on how you want to set up your user pages, you can simply comment & uncomment lines in the "Location Section"


            the first line (and default selection) sends the user to a page defined in the $users array.

            the second line directs the user to a page called "page1.htm" (or page2 or page3, etc...

            the third line directs the user to a page called <username>.htm

            the fourth (and probably best), sends the user to a page called:
            <username>-<password>.htm


            There still needs to be more error checking/handling, but the basics are in place. Obviously, btw, you'll have to actually create whatever page you redirect the user to....

            HTH,
            -Celt
            Last edited by Celtboy; Feb 8, 2004, 01:48 PM.

            Comment


            • #7
              Great!

              Thanks for the code! I will certainly work on it (and examine it to understand what is actually happening), and eventually implement it. Thanks alot!

              btw, I just bought a php/mySQL tutorial book/cd-rom toady, and by the looks of it, knowing them will definitely be invaluable in the future. Gone are the days of static html and plain old Javascript for me (although the will always have their uses).

              Thanks for the help.

              MTD

              Comment

              Working...
              X