2017-09-03 6 views
0

私はJavaScript:PHPセッションが終了して、Apacheがクラッシュすることがあるのはなぜですか?

//Function that gets the chat from backend 
function showmessage(str) { 
    if (str == "") { 
     return; 
    } else { 
     if (window.XMLHttpRequest) { 

      xmlhttp = new XMLHttpRequest(); 
     } else { 

      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       document.getElementById("chat").innerHTML = this.responseText; 
      } 
     }; 
     xmlhttp.open("GET","/backend-display.php?q="+str,true); 
     xmlhttp.send(); 
    } 
} 

//Show any messages that will pop-up 
setInterval('showmessage()',400); 


//Function that updates new rows 
function newrows(str) { 
    if (str == "") { 
     return; 
    } else { 
     if (window.XMLHttpRequest) { 

      xmlhttp = new XMLHttpRequest(); 
     } else { 

      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       document.getElementById("test").innerHTML = this.responseText; 
       var elem = document.getElementById('chat'); 
       elem.scrollTop = elem.scrollHeight; 
      } 
     }; 
     xmlhttp.open("GET","/test2.php?success=true"+str,true); 
     xmlhttp.send(); 
    } 
} 

//Updates new rows every x seconds 
setInterval('newrows()',300); 


//Backend to send a message 
function loadDoc() { 
    var xhttp = new XMLHttpRequest(); 

    var mes = document.getElementById("message").value; 
    var message = "message=" +mes; 

    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     document.getElementById("input").innerHTML = this.responseText; 
    } 
    }; 
    xhttp.open("POST", "/backend-input.php", true); 
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    xhttp.send(message); 
    document.forms["form"].reset(); 
} 

のphp.iniの設定:入力するための

Php.ini config link

バックエンド:DBからメッセージが供給ため

<?php include 'auth.php';?> 
<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 

<?php 
$name = $_SESSION["name"]; 
$messageunfilter = $_POST["message"]; 

$con = mysqli_connect('localhost','root','','chat'); 
if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
} 


if(empty($_POST["message"])){ 
      echo "You must enter a message..."; 
      exit(); 
     }else{ 
      echo "success"; 
     } 


//Checking SQL  
$check = array("\\", "'"); 
$change = array("\\\\", "''"); 

$messagefilter = str_replace($check, $change, $messageunfilter); 

date_default_timezone_set('Europe/London'); 
$current_date = date("Y-m-d H:i:s"); 

mysqli_select_db($con,"ajax_demo"); 
$sql="INSERT INTO `chat` (`id`, `username`, `message`, `date`) VALUES (NULL, '$name', '$messagefilter', '$current_date')"; 
$result = mysqli_query($con,$sql); 

mysqli_close($con); 
?> 
</body> 
</html> 

バックエンド:

<?php 
session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<body> 
<head> 
<link href="style.css" rel="stylesheet"> 
</head> 
<?php //Selects all of the logged in users messages. 
$name = $_SESSION["name"]; 

$con = mysqli_connect('localhost','root','','chat'); 
if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
} 

mysqli_select_db($con,"ajax_demo"); 
$sql="SELECT * FROM `chat` ORDER BY date"; 
$result = mysqli_query($con,$sql); 
$numrows = mysqli_num_rows($result); 

if($numrows == "0" or !isset($_SESSION["name"])){ 
      echo "<div class='msg'>"; 
      echo "<div class='username_admin'>System</div>"; 
      echo "<div class='msg_admin'>There are no messages to display...</div>"; 
      echo "</div>"; 
      exit(); 
     }else{ 
      echo ""; 
     } 


echo "<div class='msg_container'>"; 
while($row = mysqli_fetch_array($result)) { 
    $class_msg = "msg"; 
    $class_username = "username"; 
    $class_message = "message"; 

    if ($row['username'] == $_SESSION['name']) { 
    $class_msg = "msg_user"; 
    $class_username = "username_user"; 
    $class_message = "message_user"; 
    } 

    echo "<div class='$class_msg'>"; 
    echo "<div class='$class_username'><span>" . $row['username'] . "</span></div>"; 
    echo "<div class='$class_message'><span>" . $row['message'] . "</span></div>"; 
    echo "</div>"; 
    } 
echo "</div>"; 
mysqli_close($con); 
?> 
</body> 
</html> 

私はWebSocketを認識していますし、私のコードを整理しておく必要があります。

このシステムを5分程度実行した後に何らかの理由でセッションが破損しているようですか?

これはなぜわからないのですか?私は何度もそれを要求しているからですか?

メッセージが2人しか接続されていてもクラッシュしても、1分後にクラッシュする可能性はありますか?

誰かが私がなぜこれが理由であるかを理解するのに役立つことができますか、私は感謝以上のものになるでしょう。

この投稿をよくご覧いただきありがとうございます、それでも多くのことを意味します。 (ここでは、コードの過負荷のために申し訳ありませんが、私はちょうど私があなたに私ができることはすべてを示していますことを確認したい!)

+0

@MiSAKACHi主な問題は、私のセッションは破棄取得しておくことである可能性のあるエラーメッセージ – MiSAKACHi

+0

のためのApacheのログを参照してください、私はそれが今までに一度だけクラッシュしたとしてApacheクラッシュが何か他のものをどうするだったと思います! – Galliger

答えて

0

session_start()のためのPHPのドキュメントでのコメント(http://php.net/manual/en/function.session-start.php)によれば、書き込みをする必要があるかもしれませんいくつかの状況下でセッションを存続させるためにセッションデータに追加する。

あなたのsession_start()の後に$_SESSION['time'] = time();を追加してみてください。私はクラッシュを引き起こしているか分からないが、私は最初にApacheのエラーログをチェックするだろう。

+0

これはどのようにセッションを生かしておくのに役立つでしょうか、もう少し私に説明してください、ありがとう! – Galliger

+0

コメントとあなたの記述した振る舞いに基づいて推測しますが、コメント投稿者の推論はファイルシステムのマウントパラメータとatimeの代わりにmtimeが更新される問題です。各リクエストのセッションデータに明示的に書き込むことで、atimeが更新されるはずです(おそらく)。 – jconder

関連する問題