问题描述:

I'm attempting to build a login page for my website. I have managed to create a database with a simple username and password column. And have used Dreamweaver in order to create the login form. My problem is that I want users to be redirected to different pages based off of who they are. Example: if Bob (username: bob123) logs in I want him to be redirected to page x, but if Joe (username: joe123) logs in I want him to be redirected to page y. Dreamweaver only lets me specify one page.

Does anyone know any code that would redirect users based off their username in a database.

My HTML and CSS skills are decent, but I know almost nothing when it comes to this sort of thing. Any help would be greatly appreciated.

Thanks!

Here is the php code that I have so far (it was made by Dreamweaver, not me).

<?php

// *** Validate request to login to this site.

if (!isset($_SESSION)) {

session_start();

}

$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_GET['accesscheck'])) {

$_SESSION['PrevUrl'] = $_GET['accesscheck'];

}

if (isset($_POST['username'])) {

$loginUsername=$_POST['username'];

$password=$_POST['password'];

$MM_fldUserAuthorization = "";

$MM_redirectLoginSuccess = "http://------------";

$MM_redirectLoginFailed = "http://-------------";

$MM_redirecttoReferrer = false;

mysql_select_db($database_-------------, $---------);

$LoginRS__query=sprintf("SELECT Username, Password FROM userdatabase WHERE Username=%s AND Password=%s",

GetSQLValueString($loginUsername, "int"), GetSQLValueString($password, "int"));

$LoginRS = mysql_query($LoginRS__query, $----------------) or die(mysql_error());

$loginFoundUser = mysql_num_rows($LoginRS);

if ($loginFoundUser) {

$loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}

//declare two session variables and assign them

$_SESSION['MM_Username'] = $loginUsername;

$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && false) {

$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];

}

header("Location: " . $MM_redirectLoginSuccess );

}

else {

header("Location: ". $MM_redirectLoginFailed );

}

}

?>

网友答案:

In PHP you redirect to another page by sending a header containing the new location:

header("Location: http://www.example.com");

A simple but not very scaleable solution is to replace:

header("Location: " . $MM_redirectLoginSuccess );

with:

if ($loginUsername === 'bob123') {
    header("Location: http://www.example.com/bobs_page.html");
} else if ($loginUsername === 'joe123') {
    header("Location: http://www.example.com/joes_page.html");
} else {
    header("Location: " . $MM_redirectLoginSuccess );
}
网友答案:

Well, I don't use DreamWeaver (I do have it installed on my box since it came with CS5 Web Premium... never once fired it up), but my suggestion would be to store each user's preferred login page in your database, get the value from the database, and change $MM_redirectLoginSuccess accordingly.

This will require some knowledge of PHP and MySQL. No time like the present to learn :P

相关阅读:
Top