当我登录我的index.php,然后转到另一个页面,如members.php时,我又是一位客人。我怎么能这样做他们登录每个网站?我知道会议涉及,但无法弄清楚。需要任何帮助!
编辑:好的,所以我尝试了一些你们评论过的内容,这是我的最终代码:我的index.php
<?php
session_start();
?>
<html>
<!-- HTML5 Boilerplate -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine & Chrome Frame -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></title>
<meta name="description" content="This is the Responsive Grid System, a quick, easy and flexible way to create a responsive web site.">
<meta name="keywords" content="responsive, grid, system, web design">
<meta name="author" content="www.grahamrobertsonmiller.co.uk">
<meta http-equiv="cleartype" content="on">
<link rel="shortcut icon" href="/favicon.ico">
<!-- Responsive and mobile friendly stuff -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Stylesheets -->
<link rel="stylesheet" href="css/html5reset.css" media="all">
<link rel="stylesheet" href="css/col.css" media="all">
<link rel="stylesheet" href="css/2cols.css" media="all">
<style type="text/css">
/*
========================
- Note: These styles are just to pretty the basic page up a bit.
You should ignore these when copying and pasting into your site
because your stylesheet should take care of making it look pretty!
========================
*/
body { padding:2em; font : 100%/1.4 'Roboto Condensed', Helvetica, sans-serif;
}
h1 { font-size:2.2em; padding:0 0 .5em 0; }
h2 { font-size:1.5em; }
.header { padding:1em 0; }
.col { background: black; color: white; padding: 1% 0; text-align: center;}
</style>
<link rel="stylesheet" href="demo.css">
<link rel="stylesheet" href="header-fixed.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=Cookie' rel='stylesheet' type='text/css'>
<link href="http://fonts.googleapis.com/css?family=Roboto+Condensed:400,300,700" rel="stylesheet" type="text/css">
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
m = checkTime(m);
document.getElementById('txt').innerHTML =
h + ":" + m;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<header class="header-fixed">
<div class="header-limiter">
<h1><a href="#"><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></a></h1>
<nav>
<?php
$navigasjon = fopen("navigasjon.txt", "r") or die("En error har oppstått!!");
echo fread($navigasjon,filesize("navigasjon.txt"));
fclose($navigasjon);
?>
</nav>
</div>
</header>
<!-- You need this element to prevent the content of the page from jumping up -->
<div class="header-fixed-placeholder"></div>
<div class="headbanner">
<center>
<div class="headBannerText">
<?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></center>
</div>
</div>
<div class="infonav">
<div class="infonavText">
<center>
<i><div style="float: left;">Teamspeak 3: <?php
$teamspeak = fopen("teamspeak.txt", "r") or die("En error har oppstått!!");
echo fread($teamspeak,filesize("teamspeak.txt"));
fclose($teamspeak);
?></div></i>
<i><div style="float: right;"><span id="demo"></span> <span id="txt"></span></div></i>
<div style="width: 60%;"><marquee behavior="scroll" direction="right"><?php
$infonavb = fopen("infonavb.txt", "r") or die("En error har oppstått!!");
echo fread($infonavb,filesize("infonavb.txt"));
fclose($infonavb);
?></marquee></div>
</center>
</div>
</div>
<script>
var day;
switch (new Date().getDay()) {
case 0:
day = "Søndag";
break;
case 1:
day = "Mandag";
break;
case 2:
day = "Tirsdag";
break;
case 3:
day = "Onsdag";
break;
case 4:
day = "Torsdag";
break;
case 5:
day = "Fredag";
break;
case 6:
day = "Lørdag";
}
document.getElementById("demo").innerHTML = day;
</script>
<!-- HEADER SLUTTER HER -->
<center>
<br><br>
<div id="wrapper">
<div id="right">
<div class="parent">
<div class="main">
<p>Månedens klanspiller</p>
<div class="content" style="font-size: 19px;">
<?php
$topspiller = fopen("topspiller.txt", "r") or die("En error har oppstått!!");
echo fread($topspiller,filesize("topspiller.txt"));
fclose($topspiller);
?>
<style>
.list-group {
padding-left: 0;
margin-bottom: 15px;
}
.list-group-item {
position: relative;
display: block;
padding: 7px 0px;
background-color: #fff;
border: 1px solid #ddd;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
margin: auto;
}
.badge {
float: right;
min-width: 10px;
margin-right: 2%;
padding: 3px 4px;
font-size: 13px;
font-weight: 700;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: middle;
background-color: #01A9DB;
border-radius: 10px;
}
input[type=text] {
padding:5px;
border:2px solid #01A9DB;
-webkit-border-radius: 5px;
border-radius: 1px;
}
input[type=text]:focus {
border-color: #009EDC;
}
input[type=password] {
padding:5px;
border:2px solid #01A9DB;
-webkit-border-radius: 5px;
border-radius: 1px;
}
input[type=password]:focus {
border-color: #009EDC;
}
input[type=submit] {
padding: 5px 15px;
background: #01A9DB;
border: 0 none;
cursor: pointer;
-webkit-border-radius: 1px;
border-radius: 1px;
color: white;
display: inline-block;
font: 15px 'Roboto Condensed', Helvetica, sans-serif;
}
.knapp {
padding: 5px 15px;
background: #01A9DB;
border: 0 none;
display: inline-block;
cursor: pointer;
-webkit-border-radius: 1px;
border-radius: 1px;
color: white;
font: 15px 'Roboto Condensed', Helvetica, sans-serif;
}
</style>
</div>
</div>
</div>
<div class="parent">
<div class="main">
<p>Klan Info</p>
<div class="content">
<?php
$klaninfo = fopen("klaninfo.txt", "r") or die("En error har oppstått!!");
echo fread($klaninfo,filesize("klaninfo.txt"));
fclose($klaninfo);
?>
</div>
</div>
</div>
<div class="parent">
<div class="main">
<p>Logg inn</p>
<div class="content">
<?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Brukernavn: <input type="text" name="username" style="margin-top: 1%;" /><br />
Passord: <input type="password" name="password" style="margin-top: 1%;" /><br />
<input type="submit" name="submit" value="Login" class="knapp" style="margin-top: 1%;" />
</form>
<br>
Har du ikke en bruker enda?
<form action="register.php" method="get">
<input type="submit" name="register" value="Registrer">
</form>
<?php
} else {
require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
$username = $_POST['username'];
$password = $_POST['password'];
$_SESSION['username'] = $username;
$sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if (!$result->num_rows == 1) {
echo "Invalid username/password combination";
} else {
echo "<i>Du har logget inn som ";
echo $_POST['username'];
echo "</i>";
?>
<br>
<form action="index.php" method="get">
<input type="submit" name="on" value="Logg ut">
</form>
<?php
function onFfunc(){
session_destroy();
}
if(isset($_GET['on'])) {
onFfunc();
}
}
}
?>
</div>
</div>
</div>
</div>
<div id="left">
<?php
include "fusion/upload/news.php";
?>
</div>
</div>
<div class="footer"><div class="footerText">
<b><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></b> Bedwars Clan | GommeHD & PvPHeroes<br>
Copyright © 2017 | Coded by Thesevs
</div></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var showHeaderAt = 150;
var win = $(window),
body = $('body');
// Show the fixed header only on larger screen devices
if(win.width() > 600){
// When we scroll more than 150px down, we set the
// "fixed" class on the body element.
win.on('scroll', function(e){
if(win.scrollTop() > showHeaderAt) {
body.addClass('fixed');
}
else {
body.removeClass('fixed');
}
});
}
});
</script>
</center>
</body>
</html>
我的members.php
<?php
session_start();
$_SESSION['username'] = $username;
if(isset($_SESSION['username'])){
?>
<!DOCTYPE html>
<!-- HTML5 Boilerplate -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine & Chrome Frame -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></title>
<meta name="description" content="This is the Responsive Grid System, a quick, easy and flexible way to create a responsive web site.">
<meta name="keywords" content="responsive, grid, system, web design">
<meta name="author" content="www.grahamrobertsonmiller.co.uk">
<meta http-equiv="cleartype" content="on">
<link rel="shortcut icon" href="/favicon.ico">
<!-- Responsive and mobile friendly stuff -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Stylesheets -->
<link rel="stylesheet" href="css/html5reset.css" media="all">
<link rel="stylesheet" href="css/col.css" media="all">
<link rel="stylesheet" href="css/2cols.css" media="all">
<style type="text/css">
/*
========================
- Note: These styles are just to pretty the basic page up a bit.
You should ignore these when copying and pasting into your site
because your stylesheet should take care of making it look pretty!
========================
*/
body { padding:2em; font : 100%/1.4 'Roboto Condensed', Helvetica, sans-serif;
}
h1 { font-size:2.2em; padding:0 0 .5em 0; }
h2 { font-size:1.5em; }
.header { padding:1em 0; }
.col { background: black; color: white; padding: 1% 0; text-align: center;}
</style>
<link rel="stylesheet" href="demo.css">
<link rel="stylesheet" href="header-fixed.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=Cookie' rel='stylesheet' type='text/css'>
<link href="http://fonts.googleapis.com/css?family=Roboto+Condensed:400,300,700" rel="stylesheet" type="text/css">
</head>
<body>
<header class="header-fixed">
<div class="header-limiter">
<h1><a href="#"><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></a></h1>
<nav>
<?php
$navigasjon = fopen("navigasjon.txt", "r") or die("En error har oppstått!!");
echo fread($navigasjon,filesize("navigasjon.txt"));
fclose($navigasjon);
?>
</nav>
</div>
</header>
<!-- You need this element to prevent the content of the page from jumping up -->
<div class="header-fixed-placeholder"></div>
<div class="headbanner">
<center>
<div class="headBannerText">
<?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></center>
</div>
</div>
<div class="infonav">
<div class="infonavText">
<center>
<i><div style="float: left;">Teamspeak 3: <?php
$teamspeak = fopen("teamspeak.txt", "r") or die("En error har oppstått!!");
echo fread($teamspeak,filesize("teamspeak.txt"));
fclose($teamspeak);
?></div></i>
<i><div style="float: right;"><span id="demo"></span> <span id="txt"></span></div></i>
<div style="width: 60%;"><marquee behavior="scroll" direction="right"><?php
$infonavb = fopen("infonavb.txt", "r") or die("En error har oppstått!!");
echo fread($infonavb,filesize("infonavb.txt"));
fclose($infonavb);
?></marquee></div>
</center>
</div>
</div>
<script>
var day;
switch (new Date().getDay()) {
case 0:
day = "Søndag";
break;
case 1:
day = "Mandag";
break;
case 2:
day = "Tirsdag";
break;
case 3:
day = "Onsdag";
break;
case 4:
day = "Torsdag";
break;
case 5:
day = "Fredag";
break;
case 6:
day = "Lørdag";
}
document.getElementById("demo").innerHTML = day;
</script>
<!-- HEADER SLUTTER HER -->
<center>
<br><br>
<div id="wrapper">
<div id="right">
<div class="parent">
<div class="main">
<p>Månedens klanspillere</p>
<div class="content" style="font-size: 19px;">
<?php
$topspiller = fopen("topspiller.txt", "r") or die("En error har oppstått!!");
echo fread($topspiller,filesize("topspiller.txt"));
fclose($topspiller);
?>
<style>
.list-group {
padding-left: 0;
margin-bottom: 15px;
}
.list-group-item {
position: relative;
display: block;
padding: 7px 0px;
background-color: #fff;
border: 1px solid #ddd;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
margin: auto;
}
.badge {
float: right;
min-width: 10px;
margin-right: 2%;
padding: 3px 5px;
font-size: 12px;
font-weight: 700;
line-height: 1;
color: #fff;
text-align: center;
white-space: nowrap;
vertical-align: middle;
background-color: #777;
border-radius: 10px;
}
</style>
</div>
</div>
</div>
<div class="parent">
<div class="main">
<p>Klan Info</p>
<div class="content">
<?php
$klaninfo = fopen("klaninfo.txt", "r") or die("En error har oppstått!!");
echo fread($klaninfo,filesize("klaninfo.txt"));
fclose($klaninfo);
?>
</div>
</div>
</div>
</div>
<div id="left">
<?php
$stab = fopen("stab.txt", "r") or die("En error har oppstått!!");
echo fread($stab,filesize("stab.txt"));
fclose($stab);
?>
</div>
</div>
<div class="footer"><div class="footerText">
<b><?php
$klan = fopen("klan.txt", "r") or die("En error har oppstått!!");
echo fread($klan,filesize("klan.txt"));
fclose($klan);
?></b> Bedwars Clan | GommeHD & PvPHeroes<br>
Copyright © 2017 | Coded by Thesevs
</div></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var showHeaderAt = 150;
var win = $(window),
body = $('body');
// Show the fixed header only on larger screen devices
if(win.width() > 600){
// When we scroll more than 150px down, we set the
// "fixed" class on the body element.
win.on('scroll', function(e){
if(win.scrollTop() > showHeaderAt) {
body.addClass('fixed');
}
else {
body.removeClass('fixed');
}
});
}
});
</script>
</center>
</body>
</html>
<?php
} else{
header("Location: index.php");
}
?>
但这仍然不起作用,我做错了什么?
答案 0 :(得分:1)
首先,您需要使用以下代码在登录时设置会话:
session_start();
然后使用$_SESSION
,您可以检查用户是否有会话并登录。
If(isset($_SESSION)){
//loged in code
} else{
//other code
}
确定会话变量:
$_SESSION['mySessionName'] = "yourvalue";
比你可以检查是否用
设置if(isset($_SESSION['mySessionName']))
(来自Richard的评论)
有关会话的更多信息,请转到http://php.net/manual/en/book.session.php
希望这有帮助
答案 1 :(得分:1)
您可能希望使用PHP会话。
一旦您登录,您开始会话。
有一个超级全球$ _SESSION。您可以将会话数据保存到此。
这是持久性的,并且在您取消注册您的变量或结束您的会话之前保持不变。
初学者会犯的一个常见错误就是忘记了session_start()方法。
要使用会话超级全局,必须调用session_start()函数。
根据我的理解,你似乎是一个有会话的初学者。您可以在w3schools.com上查看PHP会话。 但是,一旦您对会话有了基本的了解,请确保继续学习其他内容。
希望这有帮助!