Jump to content

Server Side Toggle Switch


SJF

Recommended Posts

This concept is so simple yet I can't find anywhere that says how to do it.  The premise is simple:

 

A switch that says "on".  When you click it, it says "off".  The key is, anyone can visit the page, and see this, click it, and it can change.  Thus it has to be some sort of database or something.

 

One very jimmy-rigged thing I tried (but failed) at doing was this:

 

<?php
$myFile = "test.txt";
$fh = fopen($myFile, 'w+');
$theData = fgets($fh);


if ( $theData == "on" ) {
echo "on";

	$stringData = "off";
	fwrite($fh, $stringData);
	fclose($fh);

} else {
echo "off";

	$stringData = "on";
	fwrite($fh, $stringData);
	fclose($fh);

	}
?>

 

and in test.txt I have "off".  This way, if the site is visited, it checks to see the text file's content says "on", if it does, it echos "on", then is SUPPOSED to set the text file content to say "off", but for some reason it's not writing back to the text file.

 

Anyway there has to be a simple way to do a simple toggle button.  Thoughts? :confused:

Link to comment
Share on other sites

When you run into this situation, always make sure the variable you're checking is what you want it to be.

 

echo out $theData

 

To find out why this is happening, check out what happens when you fopen() a file with w+ as the mode.

 

 

Once you've done that, check out this code

 

<?php
$myFile = "test.txt";
$theData = file_get_contents($myFile);

if ( $theData == "on" ) {
echo "on";
$stringData = "off";
} else {
echo "off";
$stringData = "on";	
}

$fh = fopen( $myFile, 'w' );
fwrite( $fh, $stringData );
fclose( $fh );
?>

Link to comment
Share on other sites

PERFECT!!! The reason I need this is because I need a notification saying if someone is currently on the page or not (not like a counter, but simply an "occupied").

 

So for instance... if I visit the page, and no one else has it open, it will say "Available".  And if, while I am on that page, someone else visits the same page (or, to test, I could visit in another browser/window), it would say "occupied".

 

is this possible?

 

Thanks again for your help!

Link to comment
Share on other sites

Hmmm, looks like you'd need something else maybe browser-side that would flag a disconnect more so than a connection.

 

Ex.

 

Say a person visits your site, this does all the stuff that says "Hey someone is connected". If they leave the page, or close the browser, nothing is there to say "They left the site". In my limited understanding of php, it is server side, so whatever they do on their end, unless they click a logout button, your site won't know. Possibly something like Javascript would work better, or something from the server that could check a connection at an interval.

 

Either way I'll be watching this post, in case a good idea does come up.

Link to comment
Share on other sites

Pretty much impossible to verify. You'd need client-side verification, and that can never be relied on.

 

If you don't mind forcing JavaScript to be enabled, create an AJAX call that executes every 10 seconds. When a new user logs in, if that page hasn't been visited in say, 20 seconds, you know the site is available.

 

You could do this server-side by forcing a user to browse to a new page/refresh or use a meta-refresh every 60 seconds. After 90 seconds or so of inactivity, the site will be available for another user. You could use PHP sessions to track if a new user or the same user has made the request.

 

Give it a shot. Show me some code. If you can't figure it out, I'll code a quick example for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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