使用下一页中上一页的现有变量和数据库连接

时间:2014-11-30 11:53:51

标签: php mysql dreamweaver

在我的登录页面中,我使用Dreamweaver CC Server Behaviors创建登录验证(sql连接的名称是user_information)和新会话。现在,我想在下一页中使用用户名(已登录的用户)。

我该怎么办?数据库名称是登录。

以下是登录页面的代码:

<?php require_once('../Connections/user_information.php'); ?>
<?php
mysql_select_db($database_user_information, $user_information);
$query_user_login = "SELECT * FROM username";
$user_login = mysql_query($query_user_login, $user_information) or die(mysql_error());
$row_user_login = mysql_fetch_assoc($user_login);
$totalRows_user_login = mysql_num_rows($user_login);
?>
<?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 = "../q1.php";
$MM_redirectLoginFailed = "login.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_user_information, $user_information);

$LoginRS__query=sprintf("SELECT username, password FROM username WHERE username=%s AND       
password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

$LoginRS = mysql_query($LoginRS__query, $user_information) 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 );
}
}
?>

1 个答案:

答案 0 :(得分:0)

好的,所以在下一页顶部附近,您需要包含<?php session_start(); ?>,然后在您想要显示用户名的位置,您需要以<?php echo $_SESSION['MM_Username']; ?>为例:

<h1> Welcome to my site, <?php echo $_SESSION['MM_Username']; ?> </h1>