gpt4 book ai didi

php: session 在服务器上实时运行时不起作用

转载 作者:行者123 更新时间:2023-11-30 00:59:18 24 4
gpt4 key购买 nike

我正在做一个PHP项目。我有一个奇怪的错误。我的 PHP 项目在本地服务器(WAMP 服务器)上运行良好。但是在将其托管在实时服务器上后,它不起作用。

问题:Login.php 页面上创建的 session 变量未在 Report.php 页面上传递值。代码如下:

登录.php

<?php
$con=mysql_connect("mysql51****************","username","password")or die(mysql_error());
$select_db=mysql_select_db("database_name",$con);
$error="";

if(isset($_POST['submit']))
{
$userid=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$sql="SELECT * FROM user_details WHERE user_name='{$userid}' AND password='{$password}' ";

$result = mysql_query($sql);
if(mysql_num_rows($result) <= 0)
{
$error="Invalid UserId or Password.";
}
else
{
//session_set_cookie_params(60*60*60, '/', '.abcxyz.com');
ini_set('session.cookie_domain','.abcxyz.com');
session_start();
//$userid=mysql_real_escape_string($_POST['abc']);
//echo($userid);
$_SESSION['user']=$userid;
$_SESSION['userid']="true";
//echo $_SESSION['userid'];
$error="Successfully Login";
//header("Location: admin.php?page=report");
/*echo '<script type="text/javascript">alert("header is not working.' . $_SESSION['user'] . '");</script>';
*/ ?>
<script type="text/javascript">
/*alert("Please select a Source And Destination Country"); */
window.location.href='admin.php?page=report';
</script>
<?php

}
}
?>

<br />

<h1 style="text-align:center">Login</h1>
<form class="login" action="login.php" method="post" name="form1" id="form1">
<p>Username:</p>
<input class="login-input" type="text" name="username" value=""/>

<p> Password : </p>
<input class="login-input" type="password" name="password" value=""/>

<p>
<p style="color:#F00; font-size:12px; font-weight:100"><?php echo htmlentities($error); ?> </p>
<input class="login-submit" type="submit" name="submit" value="Login"/>
</form>

Report.php

<?php
include("connections/Connections.php");
/*$con=mysql_connect("localhost","admin","")or die(mysql_error());
$select_db=mysql_select_db("test",$con);*/
/*session_start();
if(session_is_registered('test'))
{
echo"registered";
}
else
{
header("Location: login.php");
}*/

//$user=$_SESSION['userid'];

$user=$_SESSION['userid'];
echo '<script type="text/javascript">alert("header is not working.' .$user . '");</script>';
if($user=="true")
{
//echo " Logining Successfully.";
}
else
{
?>

<script type="text/javascript">
//alert("Please select a Source And Destination Country");
window.location.href='login.php';
</script>
<?php
//header("Location: login.php");
}
?>
<?php
/*session_start();
$user=$_SESSION['userid'];
echo($user);
if($user=="Admin")
{
echo " Logining Successfully.";*/

$sql="SELECT * FROM register ";
if(isset($_POST['btnfilter']))
{
$search_term=mysql_real_escape_string($_POST['search_text']);
$answer = $_POST['filter'];
if ($answer == "ID") {
$sql .="WHERE id= '{$search_term}' ";
}
elseif ($answer == "Name") {
$sql .="WHERE fullname Like '%{$search_term}%'";
}
elseif ($answer == "DOB") {
$sql .="WHERE dob Like '%{$search_term}%' ";
}
elseif ($answer == "Occupation") {
$sql .="WHERE occupation Like '%{$search_term}%' ";
}
else
{
echo("Pealse Enter a valid value");
}
}
elseif(isset($_POST['btnrmfilter']))
{
$sql="SELECT * FROM register";
}

$query=mysql_query($sql) or die(mysql_errno());
//}
/*else
{
header('Location: /login.php');
}*/
?>

<style type="text/css">
table
{
font-size:12px;border-bottom:1px solid #ccc;
border-left:1px solid #ccc}
td
{
padding:5px 3px;
border-top:1px solid #ccc;
border-right:1px solid #ccc}
</style>

<div class="content">
<h1>Data Reading From database.</h1>
<form id="search_form" method="post" action="">
<div class="radio">
<input type="radio" name="filter" value="ID" />ID
<input type="radio" name="filter" value="Name" />Name
<input type="radio" name="filter" value="DOB"/>DOB
<input type="radio" name="filter" value="Occupation"/>Occupation<br />
</div>
<div class="input" >
<input type="text" name="search_text" value=""/>
<input type="submit" value="FilterData" name="btnfilter" />
<input type="submit" value="RemoveFilter" name="btnrmfilter" />
</div>
</form>

<table border="0" cellpadding="0" cellspacing="0">
<colgroup>
<col width="2%" style="color:#f60" valign="middle" align="center" >
<col width="12%" >
<col width="8%" >
<col width="5%" align="center" >
<col width="10%" >

</colgroup>
<tr style="background:#eee; height:30px;">
<td>ID</td>
<td>Name</td>
<td>DOB</td>
<td>Nationality</td>
<td>Mobile No</td>
<td>Phone No</td>
<td>Email</td>
<td>Education</td>
<td>Occupation</td>
<td>Comment</td>
<td >Noofexp</td>
</tr>

<?php while($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['dob']; ?></td>
<td><?php echo $row['nationality']; ?></td>
<td><?php echo $row['mobno']; ?></td>
<td><?php echo $row['phno']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['education']; ?></td>
<td><?php echo $row['occupation']; ?></td>
<td><?php echo $row['comment']; ?></td>
<td><?php echo $row['noofexp']; ?></td>
</tr>
<?php } ?>
</table>
<h2>&nbsp;</h2>
<h3>Thanks for view.</h3>

<!-- end .content --></div>
<div class="footer">
<p>Footer</p>

<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>

编辑:

根据建议的答案,我编辑如下:

登录.php:

<?php
session_start();
$con=mysql_connect("mysql51*************","username","password")or die(mysql_error());...
...

Report.php:

<?php
session_start();
include("connections/Connections.php");...
...

但我仍然没有在 Report.php 中获取 session 变量值。

最佳答案

session_start(); 应放置在两个页面的顶部。

关于php: session 在服务器上实时运行时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com