PHP writing to a file?

software development

#1

here’s what i’m trying to do… I want to be able to goto a page, via browser, and click a radio button and hit submit… this would (in theory) write to a file and set a varable… 1 = on and 0 = off

I want to be able to read this and if it’s on, show one link and if it’s off, another…

I guess the overall question is how and can i use PHP to write to a file on my server via a form on a password protected area of my site?

thx!
clang


#2

Hi Dang

Sorry I’m not quite sure what you are trying to do. The beauty of PHP is that you don’t actually need to write any data to file. With the help of a few regular expressions, you can get different output on your next page based on user input easily.

Try looking at the PHP http://www.php.net site for more information. They have a very good and comprehensive online tutorials and documentations.

If you’re after an out-of-the-box solution. Try looking for one at http://www.hostscripts.com.

Rgds

Wil


#3

Wil –

It appears that http://www.hostscripts.com is dead… :confused:

BTW-- You may be creating some confusion here in the boards (for my puny brain, at least), since there’s a DH support person with the username “will” and yours is “wil”. Hmmm…

Maybe you could at least use an obviously-different .sig or something?

Thanks!

…Bob The Easily Confused


#4

Hey clang, I think I get what your after. Like “Wil” pointed out with php you can pass strings through the -post- method of the form. If you set the radio with the name “link1” and hit post then on the returning page, where it would have saved the file, just call a reference like:
if ($link1 == 1) {
} else {
}

But, if you want to keep a hardcopy and wish to save it to the file then you could use various saving methods. I know I wish I had good help when I learned php so I will attempt to give you some hinters with code.

Using the same basic method as above we will pass the radio button with the name link1. Now we need to open the file we will be saving to for write access. Before you will be able to write to the file it will have to have permissions set up on it, theoretically it would be 755 but I can only get it to work with 777, chmod that is. So make sure the file is out of the way and others don’t know what the name of it is. Anyway lets open the file and write to it like this:

$myFile = fopen(“links.txt”,“w”);
fwrite($myfile, “link1val=”.$link1);
fclose ($myFile);

We have now saved the data to the file. Now all we need todo is open the file and read it. Lets open the file like this:

$myFileData = file(“links.txt”);
for ($a = 0; $a <= count($myFileData) - 1; $a++) {
$myFileLine = explode("=",$myFileData[$a]);
if (strtolower($myFileData[0]) == “link1val”) {
$link1val = $myFileData[1];
}
}

Now we can reference this in the php script as $link1val. And to finish the script off…

if ($link1val == “1”) {
echo “<a href=“page.html”>mylink1\n”;
} else {
echo “you chose not to view this link…\n”;
}

Hope this helps, and if you have any other questions post away.


#5

Hi Bob

Sorry. The URI should have read:

http://www.hotscripts.com.

My mistake.

As for my handle; well my name is “Wil” with one L, unfortunately, nothing much I can do about that.

I’ll try signing off my messages a little different though – if that helps you in any way :slight_smile:

Rgds
Wil Stephens


#6

Hehe caused me some confusion too… well just don’t try to impersonate me…

I guess it’s a Good Thing I snagged ‘will’ right away.


#7

Hi, Wil…

You wrote:

[quote]Sorry. The URI should have read:

http://www.hotscripts.com.

My mistake.

[/quote]

Thanks… That’s a script site I hadn’t seen before. I’m on my way there…

[quote]As for my handle; well my name is “Wil” with one L, unfortunately,
nothing much I can do about that.

I’ll try signing off my messages a little different though –
if that helps you in any way :slight_smile:

[/quote]

See? You could do something about it!

Thanks again…

…Bob