php会话()在登录脚本中不起作用

php会话()在登录脚本中不起作用,php,session,login,Php,Session,Login,我在登录脚本中创建会话时遇到问题。会话似乎不起作用,因为我可以通过在浏览器中键入admin.php直接访问它,并且当我登录时,我可以访问其他成员的信息,这是不应该发生的。 这是我的登录脚本: include('content/config.php'); // table name $tbl_name=tablename; // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$

我在登录脚本中创建会话时遇到问题。会话似乎不起作用,因为我可以通过在浏览器中键入admin.php直接访问它,并且当我登录时,我可以访问其他成员的信息,这是不应该发生的。 这是我的登录脚本:
include('content/config.php');
// table name
$tbl_name=tablename;

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE login='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

$_SESSION["valid_user"] = $_POST["myusername"];

header("location: admin.php?name=$myusername");
}
else {
echo "Wrong Username or Password";
}


} else {  

  $myusername = "";
      $mypassword = "";

}


?>

<html>
<head>
</head>
<body>

<table width="300" border="0" align="center">
<tr><td>

<form name="form1" method="post" action="login.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3">Login</td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>

</body>
</html>
下面是admin.php:

<?
session_start();
if (!$_SESSION["valid_user"])
{
header("location:login.php");
die();
}
?>

<?php include("content/top.php"); ?>
Admin Area
<?php include("content/bottom.php"); ?>
提前谢谢

您缺少会话\u开始;功能在您的登录页面。你需要把它放在脚本的顶部

您使用session\u启动;当您想使用会话相关功能时。

您缺少会话启动;功能在您的登录页面。你需要把它放在脚本的顶部

<?php 
session_start();
include('content/config.php');
// table name
$tbl_name=tablename;

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE login='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

$_SESSION["valid_user"] = $_POST["myusername"];

header("location: admin.php?name=$myusername");
}
else {
echo "Wrong Username or Password";
}


} else {  

  $myusername = "";
      $mypassword = "";

}


?>

<html>
<head>
</head>
<body>

<table width="300" border="0" align="center">
<tr><td>

<form name="form1" method="post" action="login.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3">Login</td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>

</body>
</html>
您使用session\u启动;只要您想使用会话相关功能

<?php 
session_start();
include('content/config.php');
// table name
$tbl_name=tablename;

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE login='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

$_SESSION["valid_user"] = $_POST["myusername"];

header("location: admin.php?name=$myusername");
}
else {
echo "Wrong Username or Password";
}


} else {  

  $myusername = "";
      $mypassword = "";

}


?>

<html>
<head>
</head>
<body>

<table width="300" border="0" align="center">
<tr><td>

<form name="form1" method="post" action="login.php">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3">Login</td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>

</body>
</html>
$\u会话[有效用户]=$\u帖子[我的用户名];//您在登录页面中使用会话,但未启动会话;在你的页面顶部


$\u会话[有效用户]=$\u帖子[我的用户名];//您在登录页面中使用会话,但未启动会话;在你的页面顶部

我这样做了,但结果相同。我还尝试了if-isset$\u POST['myusername']{session\u start;也没有结果。我这样做了,但结果相同。我还尝试了if-isset$\u POST['myusername']{session_start;也没有结果我把session_start;放在开头,但结果相同,我认为我的代码有问题。我把session_start;放在开头,但结果相同,我认为我的代码有问题。