Page 1 of 1

Importing and Exporting Variables

Posted: Thu May 26, 2005 4:17 pm
by cardine
I know a bit of php (really I've code off of tutorials), but I'm stumped on this.

Is there a code that takes a text file that is uploaded onto your website, and extracts a number from it (and turns it into a variable). Then at the end of the code write over the original text file with the variable's new value?

Thanks!

Posted: Thu May 26, 2005 4:32 pm
by John Cartwright
Does this text file only include the number?

If not, post the contents of this text file.

Basically, your going to be looking at fopen(), fread(), fwrite(), fclose()
or if you have php 5, file_get_contents() and file_put_contents()

Posted: Thu May 26, 2005 4:34 pm
by cardine
The text file are only numerical values (an integer variable).

Posted: Thu May 26, 2005 4:35 pm
by John Cartwright
So there are multiple numbers? Post the contents of this file

Posted: Thu May 26, 2005 4:38 pm
by cardine
The file will start out just containing a 1. Then hopefully (if the rest of my code works how it should) I want the file to be 2. Then next time somebody loads up the page it'll run through procedures and then the file will have a value of 3. So it is one # that after each runthrough will go up by 1. (I can program it going up, i just need to program how to change that # in the text file to a variable and then export it back to the text file).

Thanks for your speedy replies! :D

Posted: Thu May 26, 2005 4:52 pm
by John Cartwright
Jcart wrote:fopen(), fread(), fwrite(), fclose()
or if you have php 5, file_get_contents() and file_put_contents()

Posted: Thu May 26, 2005 5:30 pm
by shiznatix
if you post the code you have now then we can help you a lot more

Posted: Thu May 26, 2005 6:14 pm
by cardine
Note, I've changed the username's and passwords to x's for security reasons. (Also I haven't added the break tags in yet).

Code: Select all

<?php
    if ($variable == 1) {
        print "Username xxxx Password xxxxxxx"; $variable = $variable + 1;    } else {
           }

if ($variable == 2) {
        print "Username xxxx Password xxxxxx"; $variable = $variable + 1;   } else {
            }
if ($variable == 3) {
        print "Username xxxx Password xxxxxx"; $variable = $variable + 1;   } else {
            }

?>
It'll keep on going on and on but you get the basic jist. Now I need to know how to have this variable in a test file. I want it so at the beggining of the code the # that is in the text file is what the variable (named variable) has the value of. Then at the end I want the new value of the variable to overwrite the original value of the variable.

EDIT: I'm using this code as a confirmation page for when someone signs up for an account. I don't want to bother with php forms, so I've decided to program in the username/password combinations into the login form, and just have them display a different combination everytime somebody signs up.

Posted: Thu May 26, 2005 6:32 pm
by shiznatix
maybe i dont get what your saying but you want the user to confirm their confirmation as a new user (possibly with their email address???)

if this is true then just use a mysql database to store the usernames, check if the username is already taken, then send the user a email (provided they supplied a valid email address) that says "click here to confirm"

if im right in what you want then i can (and many others) can provide the code for that

Posted: Thu May 26, 2005 6:51 pm
by cardine
No, what I'm trying to do is this:
They user will type in their first name and e-mail. Then when they hit sign-up the confirmation page will display a username/password set. So using the code above it'll just go through a list of usernames and passwords I've already hard coded, and then it'll pick the first one availible (by find what number the variable is at).

EDIT: Although if you can do it your way and it isn't that hard to set-up (if it'll be easier then the solution that I've set up) then I'm all for it. As I side note I'll also need a log-in code if we do it that way.

Posted: Thu May 26, 2005 8:40 pm
by cardine
fopen(), fread(), fwrite(), fclose()
or if you have php 5, file_get_contents() and file_put_contents()
How do you use these variables to convert the .txt file into a variable (and back again)?

Posted: Thu May 26, 2005 8:44 pm
by Ambush Commander
Well, file_get_contents returns the file as a string. So...

Code: Select all

$contents_of_file = file_get_contents($name_of_file);
//edit it
file_put_contents($name_of_files,$contents_of_file);
Check out the PHP documentation for handy functions that imitate file_get_contents and it's kindred if you don't have PHP 5.

Read the documentation