Importing and Exporting Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Importing and Exporting Variables

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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()
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Post by cardine »

The text file are only numerical values (an integer variable).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

So there are multiple numbers? Post the contents of this file
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Jcart wrote:fopen(), fread(), fwrite(), fclose()
or if you have php 5, file_get_contents() and file_put_contents()
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if you post the code you have now then we can help you a lot more
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Post 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.
cardine
Forum Newbie
Posts: 6
Joined: Thu May 26, 2005 4:13 pm

Post 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)?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
Post Reply