0

我想修改下面的脚本,以便在重定向用户时从数据库中提取更多信息。

我想要提取的额外信息与用户名密码在同一个表中,并标记为fname。我对 php 很陌生,因此非常感谢任何能帮助我指出正确方向的人。

我知道我可以使用用户名(如果我在重定向页面上的脚本中将fname替换为用户名,这确实有效)但这是一封电子邮件,因此作为对登录用户的问候看起来不太好

这是登录用户和重定向的脚本,它工作正常:

<?php

session_start();
$host="localhost";  // Host name 
$username="";       // Mysql username 
$password="";       // Mysql password 
$db_name="trydata"; // Database name 
$tbl_name="users";  // Table name

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 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql = "SELECT * 
        FROM $tbl_name 
        WHERE username='$myusername' 
        AND password='$mypassword'"; 
$result = mysql_query($sql); 

// Mysql_num_row is counting table row 
$count = mysql_num_rows($result); 

// If result matched $myusername and $mypassword,
// table row must be 1 row 
if($count == 1){ 
  // Register $myusername, $mypassword
  // and redirect to file"login_success.php" 
  $_SESSION['username'] = $myusername; 
  $_SESSION['password'] = $mypassword; 

  // get the result set from the query
  $result = mysql_fetch_array($result); 

  // get the redirect column's value
  $redirect = trim($result['redirect']);

  if ($redirect == '') {
    echo "No redirect value was set!";
  } else {
    header('Location: ' . $redirect);
    exit;
  }
} else { 
  echo "Wrong Username or Password"; 
} 

?>

这是重定向页面中未从数据库中提取 fname 的脚本:

<?php 

// start up your PHP session! 
session_start();

if ($_SESSION["username"]) {
  echo "You are logged in as ".$_SESSION['fname'];
  echo "<p>";
  echo "<a href='aapage.php'>here is the aapage</a>";
  echo "<a href='logout.php'> Click here to logout<a/>";
} else {
  header ("Location: form.php");
}

?>
4

2 回答 2

0

首先,您需要将 fname 分配给一个变量。例如,在该行之后:

$result = mysql_fetch_array($result);

您可以添加如下内容:

$_SESSION['fname'] = $result['fname'];

然后,您可以在其他页面中使用此会话变量。

于 2012-05-12T14:02:31.990 回答
0

您永远不会设置会话变量“框架”。您需要在您的登录脚本中设置它。

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count == 1){ 
    // Register $myusername, $mypassword and redirect to file"login_success.php" 
    $_SESSION['username'] = $myusername; 
    $_SESSION['password'] = $mypassword; 
    $_SESSION['frame'] = mysql_result($result,0,"whateverfnamesrowisinyourdatabase");
}
于 2012-05-12T14:05:07.117 回答