...

View Full Version : Making a new php file within a php file..



cyphix
05-15-2004, 02:16 PM
How can you do this?

If I do this..



$fd = fopen("files/$snf", "w");

fputs($fd, "<?php");
fputs($fd, "\$fileid = $sn;");
fputs($fd, "?>");

fclose($fd);

include("files/$snf");


It won't work... do you escape it someway?

Thanks!

raf
05-15-2004, 02:36 PM
i suppose you're looking for


$fd = fopen('./files/' . $snf, 'w');

fputs($fd, '<?php');
fputs($fd, '$fileid =' . $sn . ';');
fputs($fd, '?>');

fclose($fd);

include('./files/' . $snf);

So always use single quotes for strings that need to be taken literal, and concatenate your variables instead having them parsed inside doublequoted strings.
--> it will be faster
--> it's easier to read because the colourcoding clearly shows which are literals and which are variables that wil be replaced by heir assigned value

cyphix
05-15-2004, 03:17 PM
Thanks... still getting some errors tho..

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in xxxxxxxxxxxxxxxxxxxx/files/127file.php on line 1

Parse error: parse error in xxxxxxxxxxxxxxxxxxxxxxxxxx/files/127file.php on line 1

That is the new file it created.

cyphix
05-15-2004, 03:24 PM
Plus I can't get the "\n" to work either. :mad:

cyphix
05-15-2004, 04:14 PM
nm.. I figured out a much better way to do what I'm trying to achieve. ;)

missing-score
05-15-2004, 04:24 PM
would you be so kind as to share your better method :)

raf
05-15-2004, 04:49 PM
would you be so kind as to share your better method :)
or maybe explain what you are trying to achieve or where you have/had problems with and what content you're trying to write to the file.

seems to me you don't understand the difference between single and double quotes and when you should escape special characters.

cyphix
05-15-2004, 05:20 PM
Well, what I was trying to achieve was I needed to carry unique values over several different script runs..

But I just figured out that I couldn't do it that way as if I stored the info in a file there would be no way on the second run the script would know what file to refer to & I couldn't refer to the variables any longer on the 2nd run; so I store the values from the previous page in hidden input tags on the next page & then I can carry them from page to page. :D


seems to me you don't understand the difference between single and double quotes and when you should escape special characters.

I'm not very well versed in them no. :(

raf
05-15-2004, 05:28 PM
Well, what I was trying to achieve was I needed to carry unique values over several different script runs..

But I just figured out that I couldn't do it that way as if I stored the info in a file there would be no way on the second run the script would know what file to refer to & I couldn't refer to the variables any longer on the 2nd run; so I store the values from the previous page in hidden input tags on the next page & then I can carry them from page to page. :D

using sessionvariables might be easier ...

cyphix
05-15-2004, 06:12 PM
Haven't learnt about them yet heh ;)

Spookster
05-15-2004, 06:36 PM
Haven't learnt about them yet heh ;)

Well you might want to start. You are trying to reinvent the wheel.

All you need to do is



session_start()
$_SESSION['variablename'] = "value";


Now you can access that value anywhere in your site during that browser session just by doing:



session_start();
echo "The variable value was: " . $_SESSION['variablename'];

cyphix
05-15-2004, 09:42 PM
OK thanks!

When setting variables do I need to use this:



session_start()


..before every varaiable I set?

Cheers!

anarchy3200
05-15-2004, 09:57 PM
no you do not need to put this before every variable but just put



<?PHP
session_start();
?>


at the very top of each page you wish to use them on

cyphix
05-15-2004, 10:39 PM
Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum