Dr Ben Warne Posted January 1, 2007 Share Posted January 1, 2007 The code below is supposed to log two people on using a text file placing the first "player" waiting for another, once a 2nd "player" is present loading a html screen.Also i would like to then pass an array variable between them. Thanks for helping! Dr Ben<?php session_start(); if (!isset($_POST['submit'])) { echo "<form method='post'>"; echo " your name?<input type='text' name='uname'>"; echo " <input type='submit' name='submit'>"; echo "</form>"; } else { $_SESSION['uname'] = $_POST['uname']; echo "Welcome {$_SESSION['uname']}, your session id is ". session_id(); }if (file_exists (waiting.txt))fopen("battleships.html", "r+");else$file = fopen("waiting.txt", "w+");$game = fread($file, filesize("waiting.txt"));fclose($file);$game = session_id();$file = fopen("waiting.txt");fputs($file, $game);fclose($file);?> Quote Link to comment Share on other sites More sharing options...
Dr Ben Warne Posted January 1, 2007 Author Share Posted January 1, 2007 Im still very much in the dark how this code doesnt work ??? Quote Link to comment Share on other sites More sharing options...
corbin Posted January 1, 2007 Share Posted January 1, 2007 Lets break it downif (file_exists (waiting.txt)) // checks if waiting.txt existsfopen("battleships.html", "r+"); //if it exists (someone else has viewed this page) then it opens battleships.html reading it with the file pointer at the topelse //not really sure where the else clause starts and ends >.<$file = fopen("waiting.txt", "w+"); //opens waiting.txt and creates it if doesnt already exists$game = fread($file, filesize("waiting.txt")); //stores waiting.txt in $gamefclose($file); //closes the file opener$game = session_id(); //whipes the game var and changes it to the session id$file = fopen("waiting.txt"); //fopen with out the second value (it is required)fputs($file, $game); //writes $game to an invalid file pointerfclose($file); //closes $file?>So why its not working:First off, why are you feeding waiting into $game if youre going to reset the variable a few lines later?One of your fopen tags is missing the second param...And if it did work, the person who originally opened the page would see waiting still even once the other user saw battleshipsAlso, you open battleships.html, but you never do anything with it?I suggest looking up the file functions... (http://us2.php.net/manual/en/function.fopen.php you should be able to find the other in the side nav bar, or under fopen) Quote Link to comment Share on other sites More sharing options...
Dr Ben Warne Posted January 1, 2007 Author Share Posted January 1, 2007 Lets break it downif (file_exists (waiting.txt)) // checks if waiting.txt existsfopen("battleships.html", "r+"); //if it exists (someone else has viewed this page) then it opens battleships.html reading it with the file pointer at the topelse //not really sure where the else clause starts and ends >.<Starts here, and ends with the creation of the waiting text file and putting the session id into it$file = fopen("waiting.txt", "w+"); //opens waiting.txt and creates it if doesnt already exists$game = fread($file, filesize("waiting.txt")); //stores waiting.txt in $gamefclose($file); //closes the file opener$game = session_id(); //whipes the game var and changes it to the session id$file = fopen("waiting.txt"); //fopen with out the second value (it is required)fputs($file, $game); //writes $game to an invalid file pointerfclose($file); //closes $file?>So why its not working:First off, why are you feeding waiting into $game if youre going to reset the variable a few lines later?Not really sureOne of your fopen tags is missing the second param...And if it did work, the person who originally opened the page would see waiting still even once the other user saw battleshipsAlso, you open battleships.html, but you never do anything with it?I would then go on from there once i have both players 'logged on'I suggest looking up the file functions... (http://us2.php.net/manual/en/function.fopen.php you should be able to find the other in the side nav bar, or under fopen) Quote Link to comment Share on other sites More sharing options...
corbin Posted January 1, 2007 Share Posted January 1, 2007 [code=php:0]<?php session_start(); if (!isset($_POST['submit'])) { echo "<form method='post'>"; echo " your name?<input type='text' name='uname'>"; echo " <input type='submit' name='submit'>"; echo "</form>"; } else { $_SESSION['uname'] = $_POST['uname']; echo "Welcome {$_SESSION['uname']}, your session id is ". session_id(); }$sid = session_id();if (file_exists("waiting.txt")) {$file = fopen("battleships.html", "r+");$battleships = fread($file, filesize("battleships.html"));echo $battleships;}else {$file = fopen("waiting.txt", "w+");fclose($file);echo "Please wait for another person!";}fclose($file);?>[/code]This firsts prints out your form if post is not set, then it sets the $_SESSION['uname'] variable to what ever name they put in. Then it checks if their logged in, and if they are it checks if waiting.txt exists. If it exists it outputs battleships.html and stores the content of battleships.html in $battleships (you could just do include("battleships.html");). If waiting.txt doesnt exist, it creates it then tells the user to please wait for another player...I'm sure this may need further work, or may not even be what you're trying to do, but I hope it helps :p. Quote Link to comment Share on other sites More sharing options...
Dr Ben Warne Posted January 2, 2007 Author Share Posted January 2, 2007 [quote author=corbin link=topic=120593.msg494779#msg494779 date=1167684496][code=php:0]<?php session_start(); if (!isset($_POST['submit'])) { echo "<form method='post'>"; echo " your name?<input type='text' name='uname'>"; echo " <input type='submit' name='submit'>"; echo "</form>"; } else { $_SESSION['uname'] = $_POST['uname']; echo "Welcome {$_SESSION['uname']}, your session id is ". session_id(); }$sid = session_id();if (file_exists("waiting.txt")) {$file = fopen("battleships.html", "r+");$battleships = fread($file, filesize("battleships.html"));echo $battleships;}else {$file = fopen("waiting.txt", "w+");fclose($file);echo "Please wait for another person!";}fclose($file);?>[/code]This firsts prints out your form if post is not set, then it sets the $_SESSION['uname'] variable to what ever name they put in. Then it checks if their logged in, and if they are it checks if waiting.txt exists. If it exists it outputs battleships.html and stores the content of battleships.html in $battleships (you could just do include("battleships.html");). If waiting.txt doesnt exist, it creates it then tells the user to please wait for another player...I'm sure this may need further work, or may not even be what you're trying to do, but I hope it helps :p.[/quote]Will this take the original 'player' to that html page also? Quote Link to comment 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.