用cookie显示用户名

时间:2015-04-07 02:36:45

标签: php html mysql database

我在php和mysql之间创建了一个连接,我在mysql表上添加了用户名和密码,这样我就可以登录并重定向到"成员"页。它在这一点上工作,但我想做的是显示"用户名"在我的"成员"的某些地方页面与cookie。

在first.php上,在导航栏上,在blockquote中显示" User"多数民众赞成在哪里,我想用登录的用户名替换它,但用cookie,我还没有理解如何做到这一点

我的档案:

config.php

<?php
    $mysql_hostname="localhost";
    $mysql_user="root";
    $mysql_password="root";
    $mysql_database="Electricians";

    $bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or   die("Bad Connection");
    mysql_select_db($mysql_database,$bd)or die("Bad Connection");
    ?>

index.php

<?php
    header("location:login.php");
?>

的login.php

<?php
    session_start();
    include("includes/config.php");

    if($_SERVER["REQUEST_METHOD"]=="POST")
{

    $myusername=addslashes($_POST ['username']);
    $mypassword=md5(addslashes($_POST[ 'password']));



$sql="SELECT userid FROM users WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1)
{

    $_SESSION['login_admin']=$myusername;

    header("location:http://localhost:8888/offlineproject/first.php");

}
}   
    ?>

    <!DOCTYPE html>
<html>
 <head>
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">                 
     <link rel="stylesheet" type="text/css" href="css/theme.css">
     <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
     <link rel="stylesheet" type="text/csc" href="css/bootstrap-glyphicons.css">
     <link rel="stylesheet" type="text/csc" href="css/login.css">
  <title>Online Drawing for Electricians</title>
 </head>                 
 <body>             
     <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>   
            <span class="icon-bar"></span>   
            <span class="icon-bar"></span>       
                </button>              
            </div>

             <div class="navbar-header">
                 <a class="navbar-brand" href="#"><img id="logoattop" src="images/nklogo.png" alt="Logo" >Online Drawing for Electricians </a>
      </div>


        </div>
      </nav><
      <div class="jumbotron">
      <div class="container text-center">
            <h1>Online Drawing for Electricians</h1>
                <div class="container">
      <form class="form-signin" method="post">
            <h2 class="form-signin-heading">Please sign in</h2>
            <label for="inputEmail" class="sr-only">Email address</label>
            <input name="username" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
            <label for="inputPassword" class="sr-only">Password</label>
            <input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>
             </div>
           <a href="first.php">Skip</a>
        </div>
        </div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.3.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

 </body>                 
</html>

first.php

<!Doctype html>
<html>
 <head>
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">                 
     <link rel="stylesheet" type="text/css" href="css/theme.css">
     <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
     <link href="css/bootstrap-glyphicons.css"rel="stylesheet">
  <title>Online Drawing for Electricians</title>
 </head>                 
 <body>             
     <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>   
            <span class="icon-bar"></span>   
            <span class="icon-bar"></span>       
                </button>              
            </div>
            <!navbar header-->
             <div class="navbar-header">
                 <a class="navbar-brand" href="#"><img id="logoattop" src="images/nklogo.png" alt="Logo" >Online Drawing for Electricians </a>
      </div>
               <div class="collapse navbar-collapse" id="navbar-collapse">
        <ul class="nav navbar-nav navbar-right">
        <li><a href=#><span class="glyphicon glyphicon-user"></span> <User></a></li>
        <li><a href="/offlineproject/logout.php"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
      </ul>
            </div>

        </div><!End-Container-->
      </nav><!End-Nav-Bar-->
    <div class="jumbotron">
      <div class="container text-center">
          <h1>Online Drawing for Electricians</h1>
          <div class="btn-group">
                        <a href="" class="btn btn-lg btn-warning ">Search</a>
              <a href="" class="btn btn-lg btn-default ">Upload</a>

          </div>
          </div>
      </div> <!End Jumbotron-->
    <!Blockquotes-->
      <div class="container">
      <section>
          <div class= "page-header" id="feedback">
          <h2>Recent activity of <small><User></small></h2>
          </div>

          <div class="row">
          <div class="col-lg-12">
              <blockquote>
              <p>"User" Today job is to fix the cables of the central bank.</p>
              <p>Cables need to be fixed as soon as possible </p>
              <footer>NK Electrical LTD</footer>
              </blockquote>
          </div>
           <div class="col-lg-12">
              <blockquote>
              <p>Something Something 
              SomethingSomething
              SomethingSomethingSomething</p>
              <footer>Aris Con</footer>
              </blockquote>
          </div>
          </div>
          </section>
          <a href="planedit.php">Skip</a>
              </div>




<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.3.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

 </body>                 
</html>

logout.php

<?php
    session_start();
    if(session_destroy())
    {
        header("location: index.php");
    }
    ?>

3 个答案:

答案 0 :(得分:0)

仅仅因为设置了用户/通行证,您就不能假设他们是管理员,除非进行了一些进一步的检查。通过数据库中的标志或通过匹配某处的硬编码值。

所以你需要把它改成这样的东西:

$sql="SELECT userid FROM users WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1){

$row = mysql_fetch_assoc($result);

    if($row['is_admin'] == '1'){

    $_SESSION['login_admin']=$myusername;

    header("location:http://localhost:8888/offlineproject/first.php");
    exit;
}
else{
      // do something else here
}
}
} 

答案 1 :(得分:0)

尝试使用set cookie

$first_name = 'Rilakkuma';  

setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day

对于通话cookie

$_COOKIE['first_name'];

在此处查看更多信息 - &gt; http://davidwalsh.name/php-cookies

答案 2 :(得分:0)

$ first_name =&#34; user&#34 ;;

然后setcookie();用这个来设置cookie

$ COOKIE [&#39; first_name&#39;]