Results 1 to 2 of 2

Thread: php login redirecting?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    php login redirecting?

    have a table for users,consists of username and passwords.
    the content of the table is like this
    admin---adminpassword
    user----userpassword
    user2---user2password.
    i have a login.php page then i want to redirect the admin to admin.php page when he/she logs in, and the rest user to user.php page.
    so how can i do that.
    html code for login.php

    Code:
    <html>
    <body>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form1" method="post" action="checklogin.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3"><strong>Member Login </strong></td>
    </tr>
    <tr>
    <td width="78">Username</td>
    <td width="6">:</td>
    <td width="294"><input name="username" type="text" id="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="password" type="password" id="password"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table></body>
    </html>
    for checklogin.php
    Code:
    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="users"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // username and password sent from form
    $username=$_POST['username'];
    $password=md5($_POST['password']);
    
    // To protect MySQL injection (more detail about MySQL injection)
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $username and $password, table row must be 1 row
    
    if($count==1){
    // Register $username, $password and redirect to file "login_success.php"
    session_register("username");
    session_register("password");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>
    but this code redirect to login_successful.php, for both the user and the administrator. what if i want to redirect them to a different pages?
    thanks alot!

  2. #2
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: php login redirecting?

    well in your table I would add a setting that tells whether they are a user or admin. Then in your code you could do something like this:

    Code:
    $row = mysql_fetch_array($results);
    
    if($row['user_level'] == "admin")
    {
    //redirect page for admin
    }
    else
    {
    //redirect page for user
    }
    that would be assuming that you created a field in your user table that had user_level of either admin or user
    If I helped you please rate me.

Posting Permissions

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



Click Here to Expand Forum to Full Width