jordan443 Posted August 27, 2013 Share Posted August 27, 2013 (edited) Im trying to make an Instant Message box on my site. First, you type in a username then I want it to create a new html file in /Users/ called (username).html . than I want the chat box on the site to post to and display the text in Users/(username).html . I can't seem to get it to work this is what I have: test.php: <!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> <title></title> <link type="text/css" rel="stylesheet" href="style.css" /> <style type="text/css">html { background-color: #264C67; }</style> </head> <body> <? session_start(); function loginForm(){ echo' <div id="loginform"> <form action="test.php" method="post"> <p>Please enter your name to continue:</p> <label for="name">Name:</label> <input type="text" name="name" id="name" /> <input type="submit" name="enter" id="enter" value="Enter" /> </form> </div> '; } if(isset($_POST['enter'])){ if($_POST['name'] != ""){ $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name'])); } else{ echo '<span class="error">Please type in a name</span>'; } } ?> <?php if(!isset($_SESSION['name'])){ loginForm(); } else{ ?> <div id="wrapper"> <div id="menu"> <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p> <p class="logout"><a id="exit" href="#">Exit Chat</a></p> <div style="clear:both"></div> </div> <div id="chatbox"> <div id="chatbox"><?php if(file_exists("Users/" . $_SESSION['name'] . ".html") && filesize("Users/" . $_SESSION['name'] . ".html") > 0){ $handle = fopen("Users/" . $_SESSION['name'] . ".html", "r"); $contents = fread($handle, filesize("Users/" . $_SESSION['name'] . ".html")); fclose($handle); echo $contents; } ?></div> </div> <form name="message" action=""> <input name="usermsg" type="text" id="usermsg" size="63" /> <input name="submitmsg" type="submit" id="submitmsg" value="Send" /> </form> </div> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> // jQuery Document $(document).ready(function(){ //If user submits the form $("#submitmsg").click(function(){ var clientmsg = $("#usermsg").val(); $.post("posttest.php", {text: clientmsg}); $("#usermsg").attr("value", ""); return false; }); //Load the file containing the chat log function loadLog(){ var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; $.ajax({ url: "Users/" . $_SESSION['name'] . ".html", cache: false, success: function(html){ $("#chatbox").html(html); //Insert chat log into the #chatbox div var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; if(newscrollHeight > oldscrollHeight){ $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div } }, }); } setInterval (loadLog, 1000); //Reload file every 2.5 seconds //If user wants to end session $("#exit").click(function(){ var exit = confirm("Are you sure you want to end the session?"); if(exit==true){window.location = 'test.php?logout=true';} }); }); </script> <?php if(isset($_GET['logout'])){ //Simple exit message $fp = fopen("Users/" . $_SESSION['name'] . ".html", 'a'); fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>"); fclose($fp); session_destroy(); header("Location: test.php"); //Redirect the user } ?> <?php } ?> </body> </html> posttest.php: <? session_start(); if(isset($_SESSION['name'])){ $text = $_POST['text']; $fp = fopen("Users/" . $_SESSION['name'] . ".html", 'a'); fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>"); fclose($fp); } ?> Edited August 27, 2013 by jordan443 Quote Link to comment https://forums.phpfreaks.com/topic/281613-im-help/ Share on other sites More sharing options...
MDCode Posted August 27, 2013 Share Posted August 27, 2013 What part can you not get working? Quote Link to comment https://forums.phpfreaks.com/topic/281613-im-help/#findComment-1447058 Share on other sites More sharing options...
jordan443 Posted August 27, 2013 Author Share Posted August 27, 2013 I can't get it to create the file or write to it. However, When I manually make the file and write in it myself, it will display it in the chat box. Quote Link to comment https://forums.phpfreaks.com/topic/281613-im-help/#findComment-1447060 Share on other sites More sharing options...
MDCode Posted August 27, 2013 Share Posted August 27, 2013 Do you have short tags enabled on your server? If not, you will need to change <? To <?php in posttest.php Quote Link to comment https://forums.phpfreaks.com/topic/281613-im-help/#findComment-1447062 Share on other sites More sharing options...
jordan443 Posted August 27, 2013 Author Share Posted August 27, 2013 short tags work so that doesn't seem to be the problem Quote Link to comment https://forums.phpfreaks.com/topic/281613-im-help/#findComment-1447063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.