Jump to content

Passing variables


Dr Ben Warne

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/32479-passing-variables/
Share on other sites

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 >.<
$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?
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 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)
Link to comment
https://forums.phpfreaks.com/topic/32479-passing-variables/#findComment-150913
Share on other sites


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)
Link to comment
https://forums.phpfreaks.com/topic/32479-passing-variables/#findComment-150922
Share on other sites

[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.
Link to comment
https://forums.phpfreaks.com/topic/32479-passing-variables/#findComment-150928
Share on other sites

[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?
Link to comment
https://forums.phpfreaks.com/topic/32479-passing-variables/#findComment-151336
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.