Jump to content

Dr Ben Warne

Members
  • Posts

    25
  • Joined

  • Last visited

    Never

Everything posted by Dr Ben Warne

  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  :)
  14. [quote author=radalin link=topic=117290.msg478397#msg478397 date=1165241372] set a cookie on the client side like $_COOKIE['session_id'] or use a server side cookie (also called as session) $_SESSION['session_id']. Until you use session_unregister() function or the user do not close the browser $_SESSION is kept, if you use cookies then it will last till they expire. [/quote] But how do i display all the info i want?
  15. [quote author=thorpe link=topic=117290.msg478381#msg478381 date=1165239994] Search for tutorials on a login system. Thats the only way your going to get a session id to associate with a name. [/quote] I have tried, but am still unable to find any of relevance
  16. Having bought several books on the matter, i am still trying to tacle the a simple problem, and literature i have found is more complex than i need. I an simply looking for a code that can create a session id and associate it with a name, returning a screen of Welcome ......... your session id is......... and have that session id saved in a text file, any ideas? Thank you Ben
  17. Im not looking for answers, or code. Im just working out how to tackle the below, a sort of plan of action! Because i dont know how i would approach this and am trying to break it into smaller tasks to make it easier. The game is battleships! The assignment task is to implement this game using a client-server approach. The server will allow users to request to join a game (the exact mechanism is up to you). When two players have elected to play, they will each be invited to place their ships on a grid. You are urged to focus on the communications protocols to be adopted initially. This is best achieved by implementing a text based version of the game in which everything is achieved though the communication of grid coordinates and damage messages. The server should be able to support multiple games concurrently. Having developed and tested such a text-based version, you may proceed to develop appropriate user interfaces. If a graphics interface is chosen you may use a third party library that supports graphics on either the server or the client. Alternatively, it is perfectly feasible to implement the interface using a client-side scripting language to manipulate images within a grid constructed using HTML and CSS. Sound and other appropriate multimedia content may be used. Better solutions will permit basic configuration of the game. For example by allowing the number and type of ships to be specified or by defining the rule employed to determine the number of shots available at each ‘turn’. Your solutions MUST be demonstrated on machines within the School of Computing and Mathematics. If you develop any part of your solution on other machines it is your responsibility to transfer the system to departmental machines and ensure that it is working. A brief document should accompany your work. This must document your overall design particularly focussing on: The persistence mechanism used The communications protocol The software architecture The design and implementation of the user interface Thanks!
  18. For this code below <html> <head> </head> <body> <table> <tr> <th>Headlines</th><th>Message of the Day</th> </tr> <tr> <td width='50%'>Column One</td> <td width='*'> <?php     $filename = 'text.txt';     $array_of_lines  = file($filename);     foreach($array_of_lines as $line)     print $line; ?> </td> </tr> </table> </body> </html> I want to permit the first line of the text file to be loaded into a table header cell (<th>) and the remainder of the file into an associated table data (<td>) cell. I think i use an If statement, but havnt a clue what to do
  19. [quote author=redarrow link=topic=111629.msg452504#msg452504 date=1160994517] you can only hold user information and keep it as the users information if you use cookies or sessions ok. in your case you want to throw the user from one page to anther so session would work grate ok. [/quote] But i dont want to use a session yet, i wanted to use something like <form name='form1' method='get' onsubmit='forms.form1.userid.value="Abracadabra"'> <input name='userid' type='hidden'/> as an example
  20. [quote author=redarrow link=topic=111629.msg452500#msg452500 date=1160994033] Please look up session ok Remeber when using session that on all pages you must have [code] <?php session_start();?>[/code] [/quote] I didnt want to do it in a session though, i wanted to use hidden variables Sorry if i sounded strange, im learning from scratch, and want to cover everything, but am having real trouble with this
  21. [quote author=redarrow link=topic=111629.msg452494#msg452494 date=1160993335] Just send the user to another page to show there current selected items with a confirm button. Then redirect them to pay. [/quote] How do i do that? Also how does it hold the variables for the 3rd page?
  22. I know i can do the below with more than just a hidden variable function, but as a few of you know i'm learning from scratch and want to learn a step at a time. What i want is for this code <html> <head><title>Car Permit Application Form</title></head> <body> <h2>Car permit Application Form</h2> <form action='carprocess.php' method='post'> <input type='text' name='name' size=32 />Your name<br /> <input type='text' name='email' size=32 />Your Email Address<br /> <input type='text' name='address' size=32 />Your House Address<br /> Which of the following categories do you belong to ?<br/> <select name='category'> <option value='sr'>Student (Resident)</option> <option value='snr'>Student (Non-Resident)</option> <option value='str'>Staff (Resident)</option> <option value='stnr'>Staff (Non-Resident)</option> </select><br/> <input type='submit' value='Submit Application' /> </form> </body> </html> **Saved as carpark.html AND <html> <head><title>Car Parking Application Status</title></head> <body> <h2>Car Parking Application Status</h2> <?php $status=array("sr"=>"Student (Resident)",               "snr"=>"Student (Non-Resident)",               "str"=>"Staff (Resident)",               "stnr"=>"Staff (Non-Resident)"); $cat=$_POST[category]; echo "Thank you $_POST[name]. You are a $status[$cat] user."; ?> </body> </html> **Saved a carprocess.php What i want is a middle page that will ask for confirmation of the option value selected in html page, and then return the confirmation page it does currently. Thanks for the help in advance, Dr Ben
  23. Im new to PHP, and i find it intriguing, so i am teaching myself basic php language. However, I've hit a stumbling block with this particular bit of code, i want it to display the change in its highest format, no frills just straight code Thanks! <head> <title>Vending Machine</title> </head> <body> <h2>Vending Machine</h2> <?php   $price = 27;   $payment = 100;   $change = $payment - $price; while ($change >0) {   if ($change >= 50)     $result = "50";       echo "$result";         $change = $change-$result;       if ($change >= 20)       $result = "20";         echo "$result";         $change = $change-$result;     if ($change >= 10)       $result = "10";         echo "$result";         $change = $change - 10;       if ($change >= 5)       $result = "5";         echo "$result";           $change = $change - 5;       if ($change >= 2)       $result = "2";         echo "$result";           $change = $change - 2;       if ($change >= 1)       $result = "1";         echo "$result";           $change = $change - 1;   } echo "$change"; ?> </body> </html>
×
×
  • 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.