我在php和mysql之间创建了一个连接,并在mysql表上添加了用户名和密码,这样我就可以登录并重定向到"成员"页面。在这一点上它是有效的,但我想做的是在我的"会员"页面的某些地方用cookie显示"用户名"。
在first.php上,在导航栏上,blockquote上写着"用户",这就是我想用登录的用户名替换它的地方,但用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>
注销.php
<?php
session_start();
if(session_destroy())
{
header("location: index.php");
}
?>
仅仅因为设置了用户/通行证,就不能认为他们是管理员,除非进行了进一步的检查。要么通过数据库中的标志,要么通过在某个地方匹配硬编码值。
所以你需要把它改成这样:
$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
}
}
}
尝试设置cookie
$first_name = 'Rilakkuma';
setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day
对于呼叫cookie
$_COOKIE['first_name'];
在此处查看更多-->http://davidwalsh.name/php-cookies
$first_name="user";
然后设置cookie();使用这个设置cookie
$COOKIE["first_name"]