SJF Posted July 27, 2011 Share Posted July 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/242913-server-side-toggle-switch/ Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 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 ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242913-server-side-toggle-switch/#findComment-1247702 Share on other sites More sharing options...
SJF Posted July 27, 2011 Author Share Posted July 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/242913-server-side-toggle-switch/#findComment-1247707 Share on other sites More sharing options...
Gabslol Posted July 27, 2011 Share Posted July 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242913-server-side-toggle-switch/#findComment-1247712 Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242913-server-side-toggle-switch/#findComment-1248000 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.