Jump to content

Session not being started


Akenatehm

Recommended Posts

Hey Guys,

 

Im having a really frustrating problem with this set of PHP:

 

writeConversationFunctions.php

<?php
session_start();
the function writeMessage($message){
$_SESSION['messagetest'] = $message;
$chatLogFile = "log.txt";
$openChatLog = fopen($chatLogFile, 'w') or die("Failed to open Log File.");
if($message == "resetnow"){
	$message = "";
	fwrite($openChatLog, $message);	
	fclose($openChatLog);
	$_SESSION['lastMessageSize'] = 0;
}	
elseif($message == ""){
	}
else{
	$timestamp = date("h:i"); 
	if(isset($_SESSION['username'])){
		$username = $_SESSION['username'];
	}
	else{
		$username = "Anonymous";
	}
	if($message[0] == "/"){
		$commandString = stripslashes($commandString);
		$commandString = htmlentities($commandString, ENT_QUOTES, 'UTF-8');
		$commandString = substr($commandString, 1);
		$command = explode(" ",$commandString);
		switch ($command[0]){
			case "slap":
				$name = command[1];
				$commandResponse = '<p class="commandText">' ."You slap " . $name . "across the face.</p>";
				fwrite($openChatLog, $commandResponse);
				fclose($openChatLog);
			break;
		}
	}
	else{
		$message = stripslashes($message);
		$message = htmlentities($message, ENT_QUOTES, 'UTF-8');
		$message = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $message);
		$messageString = '
		<p class="message">
			<span class="timestamp">' .$timestamp . '</span>
			<span class="username">' . $username . ': </span>' 
			. $message .
		'</p>';
		fwrite($openChatLog, $messageString);
		fclose($openChatLog);
	}
}
}


writeMessage($_POST['message']);
?>

 

Simply, the session at the top will not be created for some reason.

 

The post data is sent from:

 

index.php

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<title>Chatulo.us</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 
<script> 
var n = 0;
$(document).ready(function () {
    var focused = true;
    $(window).focus(function () {
        focused = true;
        f();
    });
    $(window).blur(function () {
        focused = false;
	f();
    });
    var title = document.title;

    var f = function () {
        if (focused) {
            n = 0;
            document.title = "Chatulo.us / Home";
        }
        else { // none of that else if needed here, you're only checking is focused or not. 
            if (n == 0) { // now you're checking if its zero, so ...
                document.title = "Chatulo.us / Home";
            } else { // you need ELSE here, otherweise you'll always use this next clause
                document.title = "(" + n + ") " + "Chatulo.us / Home";
            }
        }
    };

    function playSound(soundfile) {
        document.getElementById("dummy").innerHTML = "<embed src=\"" + soundfile + "\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
    }
    $.ajax({
        type: "POST",
        url: "loginHandlers.php",
        data: {
            required_function: "checkSession"
        },
        success: function (response) {
            $('#usernameBox').html(response);
        }
    });

    $("form#sendMessageForm").submit(function () {
        var message = $('#messageInputField').attr('value');
	$('#messageInputField').val('');
        $.ajax({
            type: "POST",
            url: "writeConversationFunctions.php",
            data: {
                message: message
		},
            success: function () {
                update();

            }
        });
        return false;
    });

    function update() {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "readConversationFunctions.php",
            data: {
                required_function: "readConvo"
            },
            success: function (message) {
                if (message.newmessage == true) {
                    $('#messageBox').html(message.message);
                    playSound('sounds/pop.mp3');
                    n = n + 1;
                    f();
              }
                else if (message.newmessage === false) {
                    $('#messageBox').html(message.message);
                }
            },
            complete: function () {
                setTimeout(update, 1000)
		$("#messageBox").attr({
            scrollTop: $("#messageBox").attr("scrollHeight")
        });
            }
        });
    }
    $("form#getUsernameForm").live('submit', function () {
        var username = $('#usernameInputField').attr('value');
        $.ajax({
            type: "POST",
            url: "loginHandlers.php",
            data: {
                username: username,
                required_function: "usernameHandler"
            },
            success: function (response) {
                $('#usernameBox').html("Processing...");
                setTimeout(function () {
                    $('#usernameBox').html(response)
                }, 1000);
            }

        });
        return false;
    });

    $("span#logoutText").live('click', function () {
        $.ajax({
            type: "POST",
            url: "loginHandlers.php",
            data: {
                required_function: "removeSession"
            },
            success: function (response) {
                $('#usernameBox').html("Processing...");
                setTimeout(function () {
                    $('#usernameBox').html(response)
                }, 1000);


            }
        });
        return false;
    });
    update();
});
</script>
</head>

<body>
<form method="post" name="messageInput" id="sendMessageForm">
    	<input name="message" id="messageInputField" type="text" autocomplete="off"/>
    	<input name="submit" type="submit" value="Send"/>
    </form>
    <div id="messageBox">
    </div>
    <div id="usernameBox">
</div>
<span id="dummy"></span>
<img src="images/logo.png" width="175" height="50" alt="Logo" id="chatulouslogo"/>
</body>
</html>

 

Any help with this issue would be GREATLY appreciated!

 

Regards,

Cody

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.