php标头不能在线工作但是在我的localhost上工作.....为什么?

时间:2016-05-24 14:33:12

标签: php

这在我的本地主机上运行得非常好,但是当我在线托管时,它不会登录并且它回显成功且没有错误。请问这可能是什么原因?

<?php
session_start();
           $_SESSION['user_logged']=$user;
             $_SESSION['user_password']=$password;
                      $user = $_POST["username"];    
                        $password = $_POST["password"];


include("include/connect.php");
$msg = array();

if(isset($_POST['submit'])){

foreach($_REQUEST as $key=>$val){
 $$key=$val;
}

if(count($msg)==0){

$sql="SELECT username, password FROM admin WHERE username='$username' && password='$password'";
$res=mysql_query($sql) OR die(mysql_error());
    if(mysql_fetch_array($res)>0){
    $_SESSION['user_logged']= $user;
    $_SESSION['user_password']=$password;   
    header("location:dashboard.php");
    echo "You looged in Successfully";
    }  else{
    $msg[]='Incorrect username/password';
    }

 }

    } 
 ?>

以下是它想要重定向到的dashboard.php。

 <?php 
 include('include/connect.php');
 include('include/function.php');
 if(isset($_REQUEST['mode']) )
    {
  $mode=$_REQUEST['mode'];
if($mode == 1)
{
     $id=$_REQUEST['id'];
     $sql="DELETE FROM enquiry WHERE id='$id'";
     $result=mysql_query($sql);
    }
   }
 $msg=array();
 if(isset($_POST['submit'])){

$title=$_POST['news'];
$news_item=$_POST['news'];
if(empty($news_item)){
    $msg[]='You must enter news in the column!';
}
if(empty($title)){
    $msg[]='News Title must not be empty!';
}

    else {

    $sql = "SELECT * FROM news_file WHERE title='$title' ";
$res = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_array($res);
if($result > 0){
    $msg[] = 'News with the same title has been added already';

}  else {

 $sql = "INSERT INTO news_file (title,news,date) VALUES ('$title','$news_item',Now())";
     $result = mysql_query($sql);   

     $msg[]='News was successfully added';

}

}

 }
 ?>

1 个答案:

答案 0 :(得分:0)

试试这个。

<?php
session_start();
    /*
        These should be the other way round, as you are setting
        the session variables with variables which have not been
        initialised yet
    */
    $user = $_POST["username"];    
    $password = $_POST["password"];
    $_SESSION['user_logged']=$user;
    $_SESSION['user_password']=$password;

include("include/connect.php");
$msg = array();

if(isset($_POST['submit'])){

foreach($_REQUEST as $key=>$val){
 $key=$val; // Removed Erroneous double $
}

if(count($msg)==0){

$sql="SELECT 
        username, 
        password 
      FROM 
        admin 
      WHERE 
        username='$username' 
      AND 
        password='$password'"; 
      // MySql does not accept && as a comparison operator.
$res=mysql_query($sql);
if(!$res)
{
    var_dump(mysql_error());
    exit;
}
else
{

    if(mysql_fetch_array($res)>0)
    {
        $_SESSION['post'] = $_POST;
        while(mysql_fetch_array($res)>0)
        {
            $_SESSION['user_logged']= $user;
            $_SESSION['user_password']=$password;   
            header("location:dashboard.php");
            echo "You logged in Successfully";
        }
    }
    else
    {
        msg[]='Incorrect username/password';
    }
}
?>

查看您为dashboard.php提供的代码,您希望有一个$_POST数据,用于您重定向到的页面。在您重定向到页面的位置,您将无法从服务器检索$_POST数据。

我已经修改了上面的脚本,将$_POST数据存储在会话中,所以使用它,你应该可以通过调用$_SESSION['post']['news']来调用你的新闻,或者如果这个太长了winded,只需在dashboard.php脚本中重新分配一次POST数据,就像这样。

$post = $_SESSION['post'];

然后您可以使用$post['news']来调用它。