简体   繁体   English

使用PHP和MySQL从登录的用户中提取其他数据

[英]Pulling through additional data from logged in user with PHP and MySQL

I would like to modify the script below to pull additional information from the database when the user is redirected. 我想修改以下脚本,以在重定向用户时从数据库中提取其他信息。

The extra information I want to pull through is in the same table as username and password and is labeled fname . 我想了解的其他信息与用户名密码位于同一表中,并标记为fname I am very new to php so any help to point me in the right direction would be very much appreciated. 我对php非常陌生,因此向我指出正确方向的任何帮助将不胜感激。

I know I could use the username (and that does work if I replace fname with username in the script on the redirected page) but that is an email so does not look good as a greeting to a logged on user 我知道我可以使用用户名 (如果在重定向页面上的脚本中用用户名替换fname的话,也可以使用该用户名 ),但这是一封电子邮件,因此对登录用户的问候不太好

Here is the script to log in the user and redirect that is working fine: 这是用于登录用户并重定向的脚本,该脚本运行良好:

<?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"; 
} 

?>

Here is the script in the redirected page that is not pulling through the fname from the database: 这是重定向页面中的脚本,该脚本未从数据库中提取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");
}

?>

First you will need to assign fname to a variable. 首先,您需要将fname分配给变量。 For example, after the line: 例如,在该行之后:

$result = mysql_fetch_array($result);

You can add something like: 您可以添加如下内容:

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

You can then use this session variable in other pages. 然后,您可以在其他页面中使用此会话变量。

You are never setting your session variable 'frame'. 您永远不会将会话变量设置为'frame'。 You need to set it in your log in script. 您需要在登录脚本中进行设置。

// 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM