doudianhuo1129 2014-04-30 16:09
浏览 49
已采纳

PHP检索记录的用户名并在页面中发布

I need to show the username of the person who is currently logged into the site and a logout form.

I currently have this code:

<?php session_start();
$myusername = $_SESSION['myusername'];
if(isset($_SESSION[$myusername]) ){}
echo $myusername;
?>

myusername is the form textbox and username is the MySQL table row.

<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <header>
        <div class="header">
            <a href="#"><img class="logo" src="images/logo.png"></a>
            <h1 class="subtitle">Internal Systems</h1>
            <h4 class="slogin">Logged as: <?php session_start(); 
$myusername = $_SESSION['myusername'];
if(isset($myusername) ){ 
echo $myusername; 
}?> | <a href="/intse/logout.php">Logout</a></h4>
        </div>
    </header>

Here is the checklogin file:

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="xxxxxx"; // Mysql password 
$db_name="intse"; // Database name 
$tbl_name="users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// 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 *, username 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_register("myusername");
//session_register("mypassword"); 
$_SESSION['myusername'] = '$myusername';
$_SESSION['mypassword'] = '$mypassword';
header("location:sindex.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
  • 写回答

1条回答 默认 最新

  • dpwfh874876 2014-04-30 16:10
    关注

    Remove variable inside $_SESSION[]

    <?php session_start(); 
    $myusername = $_SESSION['myusername'];
    if(isset($myusername) &&  !empty($myusername)){ //check username is set
    echo $myusername; 
    
    }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决