Redirect Multiple Users
Results 1 to 8 of 8

Thread: Redirect Multiple Users

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6

    Redirect Multiple Users

    Hello,

    I have been trying for the last few days to figure out how to send a particular user to their own specific URL. For example, all users will use the same login.php page but when User A logs in they will be redirected to A.php and if User B logs in they will be redirected to B.php etc.

    I am totally new to php and have been using it for about 3 weeks. I am very familiar with html. So far I have managed to create a script where the above redirection works but it is not connected to a database. The redirection is all hard-coded into the script.

    This is the code which is currently working:


    PHP Code:
    <?php
    session_start
    ();
    $users['david'] = array('password' => '123''redirect' => 'david.php');
    $users['peter'] = array('password' => '321''redirect' => 'peter.php');
    if(
    array_key_exists($_POST['username'],$users)) {
        if(
    $_POST['password'] == $users[$_POST['username']]['password']) {
            
    $_SESSION['loggedIn'] = true;
            
    header('Location:'.$users[$_POST['username']]['redirect']);
            exit();
        }
    else {
        
    // invalid password
        
    header('location: login.php');
        exit;
        }
    }
    else {
        
    // invalid username
        
    header('location: login.php');
        exit;
    }
    ?>

    This works 100% and is almost exactly what I need. The user gets redirected to their own specific URL. The only problem is that the code is not connected to a database and the users, password and redirection are all hard-coded within the script. Is there anyway to adapt the above code so it would connect to a database for the information? I have phpmyadmin installed.

    Any help whatsoever would be much appreciated and I would be truly grateful if someone could please help me.

    Kind regards,

    Robert.

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    Hello Robert, and welcome to PHPBuilder.

    Have you done any reading on this? Here's an article that might help you get started.

    I'll come back with a brief critique of it soon.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,697
    Read the article and do some testing with its concepts.

    I'll make a couple of comments about it. 1 --- it's old, and the preferred SQL API is now "Mysqli" (the "I"mproved Mysql Interface). So, a call to mysql_connect() these days is usually better written "mysqli_connect". However, some hosts don't have the mysqli_ extension installed, even to this day, so you may be forced to use the older mysql_ functions instead.

    2 --- in the professional software world, die() is only used for the rawest development work. Anything that will be placed in public view should have better error handling created for it. Meaningful (to end user) and pretty error messages that display on top of a template of your site, for example.

    3 --- he doesn't show looping over the result with an array assignment. Of course, you don't always need to do that, but it's common enough.

    All for now.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thank you dalecosp for the reply and the warm welcome.

    After reading the link you posted and also watching a few videos on YouTube I have managed to come up with this bit of code. The problem is I'm just getting redirected to index.php even if I put in the correct login details.

    In phpmyadmin I have created a table called users and four fields:

    user_id - INT
    username - VARCHAR
    password - VARCHAR
    redirect - VARCHAR

    Is there something simple which you can see I have done wrong?


    PHP Code:
    <?php
    session_start
    ();

    $server mysql_connect('*****''*****''*****');
    mysql_select_db('*****'$server);

    $user_name $_GET['name'];
    $user_password $_GET['pass'];

    $qry "SELECT id, redirect
        FROM
            users
        WHERE
            username = '"
    .$user_name."' AND
            password = '"
    .$user_password."'";

    $result mysql_query($qry);

    $count = (int)mysql_num_rows($result);
    if(
    $count != 0) {

        
    $row mysql_fetch_assoc($result);
        
        
    header('Location: '.$row['redirect']);
    } else {

        
    header(Locationindex.php);
    }
    exit;
    ?>

  5. #5
    Un Re Member cretaceous's Avatar
    Join Date
    Sep 2004
    Location
    London UK
    Posts
    940
    $count = int(mysql_num_rows($result));

    rest looks kinda ok

  6. #6
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,774
    Actually cret $count = (int)mysql_num_rows($result); is just as right. In fact more so, since you are type casting, int isn't a function. See http://us3.php.net/manual/en/languag...e-juggling.php for more info.

    However, the following line should be throwing a parse error, so I imagine that you don't have error_reporting set to E_ALL and/or display_errors (development) / log_errors (production) turned on.
    PHP Code:
    header(Locationindex.php); 
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  7. #7
    Un Re Member cretaceous's Avatar
    Join Date
    Sep 2004
    Location
    London UK
    Posts
    940
    sorry - was thinking it was intval

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    Also note that you should never place user-supplied data directly into a SQL query string, else your code will be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, you must first sanitize the data such as with a function like mysql_real_escape_string() (for string data) or by using prepared statements. See security.database.sql-injection for more info.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •