duanshan1511 2013-04-28 20:52
浏览 104
已采纳

使用php和mysql登录系统

When a user logs in, how do I get all of their mysql information? I have a registering system and login system. When they log in they type their username and password, those are the only two variables i can use, because they type them in. How do I get all the other variables, not typed in by the user, for that profile? Their usernames are unique. How do i get the rest of their variables to use throughout all of my php files?

My login file:

<?
/*Use of Sessions*/
if(!session_id())
session_start();

header("Cache-control: private"); //avoid an IE6 bug (keep this line on top of the page)

$login='NO data sent';

/*simple checking of the data*/
if(isset($_POST['login']) && isset($_POST['pass']))
{

/*Connection to database logindb using your login name and password*/
$db=mysql_connect('localhost','teachert_users','dogs1324') or die(mysql_error());
mysql_select_db('teachert_users');

/*additional data checking and striping*/
$_POST['login']=mysql_real_escape_string(strip_tags(trim($_POST['login'])));
$_POST['pass']=mysql_real_escape_string(strip_tags(trim($_POST['pass'])));
$_POST['pass']=md5($_POST['pass']);
$_POST['pass']=strrev($_POST['pass']);
$_POST['pass']=md5($_POST['pass']);
$_POST['pass'].=$_POST['pass'];
$_POST['pass']=md5($_POST['pass']);


$q=mysql_query("SELECT * FROM profiles WHERE username='{$_POST['login']}' AND     password='{$_POST['pass']}'",$db) or die(mysql_error());

/*If there is a matching row*/
if(mysql_num_rows($q) > 0)
{
    $_SESSION['login'] = $_POST['login'];
    $login='Welcome back, '.$_SESSION['login'];
    $login.='</br> we are redirecting you.';
echo $login;
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=/php/learn/selectone.php">';    
    exit;    

}
else
{
    $login= 'Wrong login or password';
}

mysql_close($db);

}

//you may echo the data anywhere in the file
echo $login;

?>

I can use their login and password in all other files with the $_SESSION['var']; How do i get the rest? Like their age? or their Name? or any variable stored in my mysql files. Yes i know MD5 isn't the best, let's not turn this into a discussion on that.

------------------------edit------------------------------- I guess i'll rephrase that:

I use this:

$q=mysql_query("SELECT * FROM profiles WHERE username='{$_POST['login']}' AND password='{$_POST['pass']}'",$db) or die(mysql_error());

How do i get variables from that particular user/profile. Like their other variables, such as their name, which in my mysql is fname.

-------------------------Edit--------------------------- I have updated to:

    $mysqli = new mysqli('localhost', 'teachert_users', 'dogs1324', 'teachert_users');
if($mysqli->connect_errno) {
    echo "Error connecting to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$user = $_SESSION['login'];
$get_user_info_query = $mysqli->query("SELECT * FROM profiles WHERE username = '$user'");
if($get_user_info_query->num_rows) {
    while($get_user_info_row = $get_user_info_query->fetch_assoc()){
    if ($get_user_info_row['math']) {   
       print_r($get_user_info_row['math']);
        }        
    }
} else {
    echo 'User not found';
}

but the print_r still prints all of the user's information. not just the math information. Why?

  • 写回答

3条回答 默认 最新

  • dongshao1981 2013-04-28 21:20
    关注

    please try using mysqli or pdo, mysql_* functions are oficially deprecated

    $mysqli = new mysqli($dbserver, $dbmanager, $dbpass, $dbname);
    if($mysqli->connect_errno) {
        echo "Error connecting to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli-    >connect_error;
    }
    $user = $_SESSION['login'];
    $get_user_info_query = $mysqli->query("SELECT * FROM profiles WHERE username = '$user'");
    if($get_user_info_query->num_rows) {
        while($get_user_info_row = $get_user_info_query->fetch_assoc()){
            print_r($get_user_info_row);
        }
    } else {
        echo 'User not found';
    }
    

    this will print the entire row information returned by mysql so to use one specific field use $get_user_info_row['username'] inside the while statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系