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; 
    
    }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度