我正在为我的网站创建一个管理页面,但我发现了一个问题。我的代码与 Wamp 完美配合(并且仍在 Chrome 中工作,但不适用于其他人或其他浏览器)。所以,问题是我的网站在标头重定向后没有保存会话变量。(我想不通的另一件事是,如果我将骰子或退出放在标题之后,页面就会变成空白,您必须手动刷新。仍然只在网络服务器上。)
<?php
session_start();
$log = $_GET['logout'];
$error = false;
//logout
if($log==true){
unset($_SESSION['user']);
header('Location: '.$_SERVER['PHP_SELF']);
//die;
}
//login
if (isset($_POST['submit']) && !isset($_SESSION['user']) ){
$file="login.xml";
$xml=simplexml_load_file($file) or die ("Unable to load XMl file!");
$nope=true;
$givenuser = $_POST['username'];
$givenpass = md5($_POST['password']);
foreach($xml->user as $user){
$username = $user->username;
$password = $user->password;
if ($givenuser == $username && $givenpass == $password){
$_SESSION['user'] = $givenuser;
$nope =false;
header('Location: '. $_SERVER['PHP_SELF']);
//die();
}
}
if($nope) $error = true;
}
//succesful login
if (isset($_SESSION['user'])){
?>
<html>
<header><link rel="stylesheet" href="style.css" type="text/css" /></header>
<body>
<h1>
<?php echo "Welcome ".$_SESSION['user']; ?>
</h1>
<div>
<a href="?">New page</a>
<a href="?logout=true">Logout</a>
</div>
<div id="menus">
Pages:</br>
<?php
$file="../pages.xml";
$xml=simplexml_load_file($file) or die ("Unable to load XMl file!");
$index = 0;
foreach($xml->xpath('//oldalneve') as $pagename){
echo '<a href="?id='.$index.'">'.$pagename.'</a></br>';
$index=$index+1;
}
?>
</div>
<?php
if(!is_numeric($_GET['id'])){
$id=-1;
}else{
$id=(int)$_GET['id'];
}
if($id!=-1){
$id=(int)$_GET['id'];
if(isset($_POST['name']) && isset($_POST['body'])){
if($_REQUEST['name'] !="" && $_REQUEST['body']!=""){
$file="../pages.xml";
$xml= simplexml_load_file($file) or die ("Unable to load XML file!");
$name = $_POST['name'];
$body = $_POST['body'];
$xml->page[$id]->oldalneve = $name;
$xml->page[$id]->body = $body;
$xml->asXML('../pages.xml');
header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
//die;
}
}
$file="../pages.xml";
$xml= simplexml_load_file($file);
$nam=$xml->page[$id]->oldalneve;
$val=$xml->page[$id]->body;
?>
<div style="float:left;">
<form name="input" method="post" action="" >
<label>Name</label></br>
<input type="text" name="name" style="width:250px" value="<?php echo $nam;?>"></br>
<label>Body</label></br>
<textarea cols="28" rows="10" name="body"><?php echo $val;?></textarea></br>
<input type="submit" name="submit" value="Küldés">
</form>
</div>
</body>
</html>
<?php
}else{
?>
<div style="float:left;">
<form name="input" method="post" action="" >
<fieldset><legend>Add page</legend>
<label>Name</label></br>
<input type="text" name="name" style="width:250px"></br>
<label>Body</label></br>
<textarea cols="28" rows="10"v name="body"></textarea></br>
<input type="submit" name="submit" value="Küldés">
</fieldset>
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['submit'])){
if($_REQUEST['name'] !="" && $_REQUEST['body']!=""){
$file="../pages.xml";
$xml= simplexml_load_file($file) or die ("Unable to load XML file!");
$page=$xml->addChild('page');
$name = $_POST['name'];
$body = $_POST['body'];
$page->addChild('oldalneve', $name);
$page->addChild('body', $body);
$xml->asXML('../pages.xml');
header('Location: '.$_SERVER['PHP_SELF']);
//die;
}
}
}
}else
{
showLogin($error);
}
function showLogin($error){?>
<form action="" method='post'>
<label for="username">username</label>
<input type="text" name="username" id="username"></br>
<label for="password">password</label>
<input type="password" name="password" id="password"></br>
<?php
if($error){
echo "Invalid username or password".'</br>';
}
?>
<input type="submit" name="submit" value="submit">
</form>
<?php }
?>
用户可以登录并查看界面,但使用其中一个链接会将他重定向到登录屏幕(会话用户未保存)。
这是phpinfo():
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 4 4
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0