问题
I am working on a project of PHP. I have a strange error. My PHP project is working well on local server (WAMP server). But after hosting it on live server, it is not working.
Problem: Session variable created on Login.php
page is not passing value on Report.php
page. The code is as below:
Login.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> </h2>
<h3>Thanks for view.</h3>
<!-- end .content --></div>
<div class="footer">
<p>Footer</p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
Edit:
As the answers suggested, i edited as below:
Login.php:
<?php
session_start();
$con=mysql_connect("mysql51*************","username","password")or die(mysql_error());...
...
Report.php:
<?php
session_start();
include("connections/Connections.php");...
...
But still i am not getting the session variable value in Report.php
.
回答1:
session_start();
should be placed on top of the both pages.
回答2:
have you checked your phpinfo() ? make shure you have Session Support - enabled
回答3:
You must add session_start();
in your report.php page
回答4:
If it runs on your WAMP successfully, It is very possible that the problem is not from your code but from your hosting company.
Something like this happened to me before.
Just call you hosting company and tell them to enable session on their server for you and they will it for you.
and always start session first before any script just as you did above.
session_start();
Enjoy
回答5:
Go try adding this on your code:
if(!$con)
{
die('Could not connect:'. mysql_error());
}
and see what the error is.
回答6:
very easy solve to this problem copy this code
error_reporting(0);
session_start();
Note: give space from left side of session_start();
if you want understand about this watch this video
来源:https://stackoverflow.com/questions/20281624/php-session-not-working-while-running-live-on-server