Jump to content

Dr Ben Warne

Members
  • Posts

    25
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Dr Ben Warne's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. [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?
  2. Lets break it down if (file_exists (waiting.txt)) // checks if waiting.txt exists fopen("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 top else //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 $game fclose($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 pointer fclose($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 sure 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 battleships Also, 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)
  3. Im still very much in the dark how this code doesnt work  ???
  4. 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); ?>
  5. [quote author=Dr Ben Warne link=topic=117595.msg481888#msg481888 date=1165709433] any ideas? [/quote] Any??
  6. [quote author=Qtix link=topic=117595.msg481025#msg481025 date=1165575298] for what do you want to use it? cause its best to avoid fopen and fwrite [/quote] What i want it for one person to get a session id, and then for someone else to aswell so i can pass an array between the 2 users
  7. Is there a code that creates a file and adds to it?
  8. [quote author=kenrbnsn link=topic=117595.msg480066#msg480066 date=1165441352] Take a look at the function [url=http://www.php.net/file_exists]file_exists()[/url]. You should test that the file exists before writing to it... Ken [/quote] How do i get it to create a new file and put the session id in though?
  9. Can anyone help? Im still having no luck  >:(
  10. So far i have this code <?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();   } ?> But from that i want to, on entry, create a text file that will put the session id into it to wait for another id to join BUT i want it to search to see whether the file exists (to see if someone is already waiting) the go to a new screen I have this code previously $file = fopen("count.txt", "r+"); $count = fread($file, filesize("count.txt")); fclose($file); $count += 1; $file = fopen("count.txt", "w+"); fputs($file, $count); fclose($file); echo "You are visitor $count"; That writes to a text file, but i cant adapt it. Any help would be greatly appretiated! Ben
  11. [quote author=thorpe link=topic=117290.msg478463#msg478463 date=1165245485] Maybe this will help. [code] <?php   session_start();   if (!isset($_POST['submit'])) {     echo "<form method='post'>";     echo "  your name?<input type='text' name='uname'>";     echo "  <input type='submit'>";     echo "</form>";   } else {     $_SESSION['uname'] = $_POST['uname'];     echo "Welcome {$_SESSION['uname']}, your session id is ". session_id();   } ?> [/code] And no. Sessions only last the lifetime of a users visit. You will need to use cookies aswell if you want persistence. [/quote] This code doesnt seem to do anything  ???
  12. [quote author=thorpe link=topic=117290.msg478456#msg478456 date=1165244911] I havent done a tutorial in several years so Im sorry I don't have a link handy. Goggle however produces 19 million + hits on the subject. In its simplest terms. [code] <?php   session_start();   $_SESSION['uname'] = "thorpe";   echo "Welcome {$_SESSION['uname']}, your session id is ". session_id(); ?>[/code] [/quote] How would the new user enter their details ie nickname etc for it to the create a session id associated to it? Would that then remember the name for the next log on if the cookie is still there?
  13. [quote author=thorpe link=topic=117290.msg478446#msg478446 date=1165243998] Really.. do you want us to write yet another tutorial especially for you? [/quote] A link might be nice  :)
×
×
  • 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.