SRL Archive - Web Development

Sort of switch in PHP?


ShowerThoughts

2009-11-16 17:29:40

I think it is possible to have php script that works like this:

Domain/switch.php?switch=on (or switch=true or switch=1)
Domain/switch.php?switch=off (or switch=false or switch=0)
And then when you do a getpage, on Domain/switch.php it will give you the state of the switch in text format.
How would you do this?

Thank you for your time, I have my ideas on this, but I am not that good at php and don't know where to start. The last thing i've made in php(with somebody else -.-) is like waaaay to long ago.

mixster

2009-11-16 17:32:13

stores value after the = in the php variable $_GET['var_name']

ie moo.php?wat=3 => $_GET['wat'] holding value 3.
For multiple ones, separated by &
ie moo.php?wat=3&who=me

Sir R. M8gic1an

2009-11-16 17:33:29

$_GET[switch] or something very similar if i remember correctly.

it comes as a variable from the GET method

edit: ninjaed

~RM

Awkwardsaw

2009-11-16 17:50:04

but, if you did

getpage(website.com/wat.php?switch=on);

although the switch was off, wouldnt that not work?

Daniel

2009-11-17 04:27:56

Do you mean something that if parametre switch equals On or 1, it'll display the text "On", and same but reverse for "Off"?

If so, then you can do something like:
[php]
<?php
$switch = strtolower($_GET['switch']);
if(($switch == 'on') || ($switch == '1'))
echo "On" else
if(($switch == 'off') || (isnumeric($switch)))
echo "Off" else
echo "Invalid value";
?>[/php]

Floor66

2009-11-17 05:28:19

Neither of all the previous codes posted save the status of the switch, so every time you visit the page, $_GET['switch'] will be blank.

Don't you want something like:
[php]
<?php
function getStatus()
{
return file_get_contents("switch.txt");
}

function changeStatus($status)
{
if($status != "on" || $status != "1" || $status != "off" || $status != "0")
{
echo "Wrong switch input";
return false;
exit;
}
else
{
$f = file_put_contents("switch.txt", $status);
if(!$f)
{
echo "Error switching";
return false;
exit;
}
else
{
echo "Successfully switched";
return true;
}
}
}

if($_GET['switch']) //if there is any ?switch=x in the url
{
if($_GET['switch'] != getStatus()) //prevents setting the same as there already is in switch.txt
{
changeStatus($_GET['switch']); //obvious; does the actual switch
}
}
?>
[/php]

Now the file switch.txt will contain on/off/1/0
This way, or you'd have use a db.

Drunkenoldma

2009-11-17 06:32:58

I think he means this:
[php]<?php
$str = "Domain/switch.php?switch=on";
$strpart = explode("switch=", $str);
echo $strpart[1];
?>[/php]
This will just show everything after "switch=", and therefore the text "on".. (without the quotes ofc)

ShowerThoughts

2009-11-17 10:48:22

Thanks for the help I know how Get and Post variables work, I worked with them before I think Floor66 has made the thing I wanted, but everyone I really want to thank you guys for the feedback, normally I get stuff like: "Google STFU".

Thanks :).

mixster

2009-11-17 11:01:04

erm, if(isset($_GET['switch'])) {} not if($_GET['switch']) {} else it won't run if you pass false or 0 or another value that breaks down to false in php.

Also thinking that it's a bit of a silly way of valid input checking, but nothing wrong with it.

ShowerThoughts

2009-11-17 11:08:01

Mixster is right, I guess… it isn't working correctly.
I put home.impsoft.info/s.php & home.impsoft.info/s.txt online, try it.

Floor66

2009-11-17 12:24:02

[php]
<?php
if(isset($_GET['s']))
{
$f = file_put_contents("s.txt", $_GET['s']);
}
echo "switch = ". file_get_contents("s.txt");
?>
[/php]

ShowerThoughts

2009-11-17 12:38:30

haha, :p again it isn't working fully… it gives an error with a weird sign probably a typo.
http://home.impsoft.info/downloads/phpswitches.zip

Floor66

2009-11-17 12:43:03

Hmm I don't see anything wrong with it. Post the error?

ShowerThoughts

2009-11-17 13:02:42

Warning: file_put_contents(s.txt) [function.file-put-contents]: failed to open stream: Permission denied in /home/homeimps/public_html/s.php on line 4
switch = 1

so I've got no acces to it hmmm…..
nvm will be able to fix that I guess.

Floor66

2009-11-17 13:13:38

You have to CHMOD the text file(s) to 777.

ShowerThoughts

2009-11-17 15:54:11

From: Floor66
You have to CHMOD the text file(s) to 777.

In other words, change the permission for the text file's?

Daniel

2009-11-17 23:12:08

From: Hermen
In other words, change the permission for the text file's?


Yes, to 777. Full public and owner access: "Read, Modify, Create" (a.k.a 777).

You can do this in your FTP client, as most support it :)

EDIT: Also, i didn't realise you wanted it to save :/

Floor66

2009-11-18 02:40:30

Done, worked last time I tested ;)

rogeruk

2009-11-18 07:58:40

[php]<?php
switch($page) {
default: include("home.php"); break;
case 'home': include("home.php"); break;
case 'news': include("news.php"); break;
case 'downloads': include("downloads.php"); break;
}
?>[/php]

Floor66

2009-11-18 08:35:18

uhm… that's not at all what he needed… and if you'd have read the thread; it's done now.

ShowerThoughts

2009-11-18 16:57:02

Everyone, thank you soooo much. My program works perfectly now =).
I am going to release too probably lots of fun is this.
I can now activate the program with my mobile, run it silently and let it remove itself(remove all traces)
genious.

I kinda only need a bat that waits 5 seconds and then kills the program, and deletes the program. =)
Shouldn't be to hard.