PHP 未登陆用户打开任何网页均跳转到登陆窗口

phpchuxuecainiao 2010-08-30 11:30:09
输入子页面地址后判断是否登录,如果无登录返回index,否则跳转到index_bc

主页
<?php
session_start();
if(!isset($_cookie['username'])){
setcookie("username",$username,time()+3600*24*2);
}
include("lianjie.php");
//$_SESSION['FORMINDEX'] = "yes";
?>
<!--***************登录页面***************-->
<link href="CSS/css.css" rel="stylesheet" type="text/css">
<body >
<table align="center">
<tr><td>
<table width="800px"align="center" style="background-image:url(img/tu_beijing.jpg);height:510px; " >
<tr ><td height="55" colspan="4"><img src="img/logo01.jpg" align="right">

</td>
</tr>
<tr>
<td height="240" colspan="3"></td>
<td width="430" valign="bottom"><form name="form1" method="post" action="" onsubmit="return Checkform();" style=" text-align:center; ">
<table border="0" cellspacing="0" cellpadding="1" align="left">
<tr>
<th colspan="2" align="center" height="50px"><font color="#000000" face="华文宋体" size="+1">用户登录</font></th>
</tr>
<tr>
<td height="30">
<div align="right" style="color:#000000; font-size:13px; height:10px;">用户名:</div></td>
<td align="left">
<input type="text" name="user_login" size="20" maxlength="16" style="border-color:#666666;">
</td>
</tr>
<tr><td height="30">
<div align="right" style="color:#000000;font-size:13px;height:30px;">密 码:</div></td>
<td align="left">
<input type="password" name="user_pass" size="20" maxlength="16" style="border-color:#666666;"><p>
</td>
</tr>

<tr>
<td colspan="2">
<div align="center">
<input type="image" name="login" value="" src="img/denglu.gif">
</div></td>
</tr>
</table>
</form>
</tr>
<tr>
<td colspan="3" height="190"> </td>
<td width="420"> 
</td></tr>
</table>
</td>
</tr>
<tr><td</td></tr>
</body>
<?php
$user_login=$_POST["user_login"];//赋值
$user_pass=$_POST["user_pass"];
if($user_login && $user_pass)
{
$query="select * from denglu where user='".$user_login."' and pwd='".$user_pass."'";//从数据库查找用户名和密码
$res=mysql_query($query,$connection);

if(mysql_fetch_array($res))
{
$_SESSION['user_login']=$user_login;//传递用户名
echo $SESSION['user_login'];
echo "<script>window.location='index_bc.php';</script>";exit();//登录成功跳转到index_bc
}else{
echo "<script>alert('用户名或密码错误');window.location='index.php';</script>";exit();}
}
mysql_close($connection);
?>

子页
<?php
session_start();
/*if(!isset($_SESSION['FORMINDEX'])){
header("location: index.php");
}*/
$_SESSION['username']=$username;
if(!empty($_COOKIE['username']))
{
header("location: index.php");
}
else
{
header("location: index_bc.php");
}
?>

请大侠帮忙改改
...全文
220 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
phpchuxuecainiao 2010-08-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lms514168424 的回复:]
你要确定你是利用session还是cookie还是两者全部来判断是否登陆,然后给出条件就行了
[/Quote]
当然用session了
phpchuxuecainiao 2010-08-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lms514168424 的回复:]
登陆页

PHP code
<?php
session_start();
include("lianjie.php");
?>
<!--***************登录页面***************-->
<link href="CSS/css.css" rel="stylesheet" type="text/css">
<body >
<table align="cen……
[/Quote]
这样写登录不进去子页面了
lms514168424 2010-08-30
  • 打赏
  • 举报
回复
你要确定你是利用session还是cookie还是两者全部来判断是否登陆,然后给出条件就行了
lms514168424 2010-08-30
  • 打赏
  • 举报
回复
登陆页
<?php
session_start();
include("lianjie.php");
?>
<!--***************登录页面***************-->
<link href="CSS/css.css" rel="stylesheet" type="text/css">
<body >
<table align="center">
<tr><td>
<table width="800px"align="center" style="background-image:url(img/tu_beijing.jpg);height:510px; " >
<tr ><td height="55" colspan="4"><img src="img/logo01.jpg" align="right">

</td>
</tr>
<tr>
<td height="240" colspan="3"></td>
<td width="430" valign="bottom"><form name="form1" method="post" action="" onSubmit="return Checkform();" style=" text-align:center; ">
<table border="0" cellspacing="0" cellpadding="1" align="left">
<tr>
<th colspan="2" align="center" height="50px"><font color="#000000" face="华文宋体" size="+1">用户登录</font></th>
</tr>
<tr>
<td height="30">
<div align="right" style="color:#000000; font-size:13px; height:10px;">用户名:</div></td>
<td align="left">
<input type="text" name="user_login" size="20" maxlength="16" style="border-color:#666666;">
</td>
</tr>
<tr><td height="30">
<div align="right" style="color:#000000;font-size:13px;height:30px;">密 码:</div></td>
<td align="left">
<input type="password" name="user_pass" size="20" maxlength="16" style="border-color:#666666;"><p>
</td>
</tr>

<tr>
<td colspan="2">
<div align="center">
<input type="image" name="login" value="" src="img/denglu.gif">
</div></td>
</tr>
</table>
</form>
</tr>
<tr>
<td colspan="3" height="190"> </td>
<td width="420"> 
</td></tr>
</table>
</td>
</tr>
<tr><td</td></tr>
</body>
<?php
$user_login=$_POST["user_login"];//赋值
$user_pass=$_POST["user_pass"];
if($user_login && $user_pass)
{
$query="select * from denglu where user='".$user_login."' and pwd='".$user_pass."'";//从数据库查找用户名和密码
$res=mysql_query($query,$connection);

if(mysql_fetch_array($res))
{
$_SESSION['user_login']=$user_login;//传递用户名
$_SESSION['FORMINDEX'] = "yes";
echo "<script>window.location='index_bc.php';</script>";exit();//登录成功跳转到index_bc
}else{
echo "<script>alert('用户名或密码错误');window.location='index.php';</script>";exit();}
}
mysql_close($connection);

?>


子页
<?php
session_start();
/*if(!isset($_SESSION['FORMINDEX'])){
header("location: index.php");
}*/
if($_SESSION['FORMINDEX'] = "yes")
{
header("location: index_bc.php");
}
else
{
header("location: index_.php");
}
?>
phpchuxuecainiao 2010-08-30
  • 打赏
  • 举报
回复
在线等
Abin-2008 2010-08-30
  • 打赏
  • 举报
回复
你有点糊涂了...
如果是session判断登陆,你就单纯用session就可以了
如果是用cookie判断登陆,那你也没有必要用到session了.
看你的代码.登陆成功是用session保存, 所以就用session来判断吧.

子页不要有 session_start();在你要判断是否登录的页面写session_start();而且要在顶部.

if(isset($_SESSION['user_login']))
{
header("location: index_bc.php");
}
else
{
header("location: index_.php");
}
xintianyu 2010-08-30
  • 打赏
  • 举报
回复
登陆之后 设置一个session值
然后在每个子页中 最前面 加一个判断
判断是否设置过这个session 如果没有 则跳转到登录界面

4,250

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧