Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2007, 11:58 PM   PM User | #1
crewebiker
New to the CF scene

 
Join Date: Sep 2007
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
crewebiker is an unknown quantity at this point
undefined variable problem

i've came up with the following script for an 'easy' write to file from form page.

i've done it a few times before with success, but for some strange reason this time i'm having a bit of trouble with the variable 'page' coming up undefined. It's probably something really simple but i can't seem to be able to spot it.

PHP Code:
    if (isset($_POST['update'])){

        
$file fopen('db/pages/home.php''a'1);
        
fwrite($file$page);
        
fclose($file);
        }
    
    else {  
        
$lines file('db/pages/home.php');
        
$l_count count($lines);
        for(
$x 0$x$l_count$x++)
        {
        }
        echo 
"<form method=\"post\" name=\"update\" enctype=\"multipart/form-data\" action=\"?page=admin&mode=edit\">";
        echo 
"<textarea name=\"page\" cols=\"80\" rows=\"8\">".$lines[5]."</textarea>";
        echo 
"<input type=\"Submit\" name=\"update\" value=\"Update\"></form>";}


Any help would be greatly appreciated, thanks.
crewebiker is offline   Reply With Quote
Old 09-05-2007, 12:12 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
If I had to guess I would say the "register_globals" PHP flag has been disabled on your PHP server. This "register_globals", when it's turned on, automatically assigns all the $_POST array elements (and $_GET elements) to non-array variables (i.e. the value in $_POST['page'] is assigned to $page). It's been an infamous security hole in PHP and because of that it defaults to "off" in PHP versions 4.3+.

To be safe and cross-server operational you should just use the $_POST['page'] variable, or manually assign $_POST['page'] to $page (and other form variables) at the beginning of your script.
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
crewebiker (09-05-2007)
Old 09-05-2007, 02:43 AM   PM User | #3
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
register_globals, agreed. A form input or url name/value pair called 'page' is registering as the var $page when register_globals = On.

If it's a post, then:

$page = (!empty($_POST['page'])) ? $_POST['page'] : NULL;

will fix it, else:

$page = (!empty($_GET['page'])) ? $_GET['page'] : NULL;
__________________
I do Web Design, Brisbane based.
More time spent in PHP/MySQL Web Development.
And Search Engine Optimisation takes up the rest of it.
meth is offline   Reply With Quote
Users who have thanked meth for this post:
crewebiker (09-05-2007)
Old 09-05-2007, 02:59 PM   PM User | #4
crewebiker
New to the CF scene

 
Join Date: Sep 2007
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
crewebiker is an unknown quantity at this point
thanks guy, that's done the trick, now just another thing...

would i be able to get '$page' to only write to a specific line in '$file'?

tried using the same kinda way from the read script but can't quite get it, it comes up with 'fwrite(): supplied argument is not a valid stream resource in...'

again, help would be greatly appreciated.

thanks.
crewebiker is offline   Reply With Quote
Old 09-05-2007, 03:42 PM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You have to open the file in read/write mode and read each line in until you get to the line you want to overwrite and then write it.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:47 AM.


Advertisement
Log in to turn off these ads.