我在php中完成了登录页面,并且我将用户名存储在cookie中。但我无法在我的主页中访问这个cookie变量,这也是在php中完成的,我正在我的localhost wamp服务器上做这一切的事情有人请帮我点头
的login.php
<html>
<head>
<link type="text/css" rel="stylesheet" href="login.css"/>
<title> LOGIN Page</title>
</head>
<body>
<div id ="fullscreen">
<div id="container">
<div id="heading">LOGIN</div>
<div id="login-form" >
<form id="my-login" action="" method="POST">
<input type ="text" name ="user_name" placeholder=" Enter login id" class="text_box" id="usrname">
<label id ="lusr_name" value="" name="lusr_name"></label><br>
<input type ="password" name ="password" placeholder=" Password" class="text_box" id="paswd">
<label id ="lpassword" value="" name="password" ></label><br>
<input type ="submit" name ="login" value ="Log In" class="login_button" >
</form>
</div>
<a href="signup.php" id="my_signup">Sign Up</a>
</div>
</div>
<?php
if(isset($_POST['login']))
{
$username = $_POST['user_name'];
$pwd = $_POST['password'];
if(empty($username) || empty($pwd))
{
if(empty($username))
{
echo "<script>document.getElementById(\"lusr_name\").innerText=\"Enter username\";</script>";
echo "<script>document.getElementById(\"usrname\").className=\"text_errorbox\";</script>";
}
if(empty($pwd))
{
echo "<script>document.getElementById(\"lpassword\").innerText=\"Enter Password\";</script>";
echo "<script>document.getElementById(\"paswd\").className=\"text_errorbox\";</script>";
}
}
else
{
//setcookie( "TestCookie",$username,false,"/",false );
setcookie( 'TestCookie', $username, time()+60*60*24*7, '/', 'localhost', false, true);
include 'userdata.php';
$obj = new userdata();
$obj -> authent($username,$pwd);
}
}
?>
</body>
</html>
homepage.php
<html>
<head>
<title>Login Successful</title>
<>
</head>
<body style="background:#323B55">
<h1 style="text-align:center ;color:#ffffff;font-family:arial;">LOGIN SUCCESSFUL!!!!!!!!!</h1>
<?php
if(isset($_COOKIE['TestCookie']))
{
echo 'Welcome ' . $_COOKIE['TestCookie'];
}
?>
</body>
</html>
答案 0 :(得分:1)
我认为在设置Cookie时,应将'localhost'删除为Cookie域。把它留空吧。 ''
修改强>
嗯,等等...... setCookie()
必须在任何其他输出开始之前执行!在你的情况下,你在setCookie之前输出了很多HTML!请先执行setCookie部分,或阅读Output Buffering。
在开始输出后使用setCookie会产生通知/警告。但它可能已被压制。
答案 1 :(得分:0)
尝试使用: -
这是一个有效的例子
$value="bikash";
setcookie("TestCookie", $value, time()+3600);
echo $_COOKIE['TestCookie'];
答案 2 :(得分:0)
<?php ob_start(); ?> <html>
<head>
<title>Login Successful</title>
<>
</head>
<body style="background:#323B55">
<h1 style="text-align:center ;color:#ffffff;font-family:arial;">LOGIN SUCCESSFUL!!!!!!!!!</h1>
<?php
if(isset($_COOKIE['TestCookie']))
{
echo 'Welcome ' . $_COOKIE['TestCookie'];
}
?>
</body></html> <?php ob_end_flush(); ?>
试试这段代码