HOW do i fwrite an entire php page?

aka generate user profile with user information ect...

Page 1 of 1

13 Replies - 3253 Views - Last Post: 26 May 2009 - 06:11 PM Rate Topic: -----

#1 emopoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-May 09

HOW do i fwrite an entire php page?

Posted 24 May 2009 - 04:31 PM

i just want to know the easiiest way / how to do it like a link im not asking for u to give me a script but if anyone knows how.
i have a registration script once the person registers,
the script makes a directory that is their username and then and index.php file in the directory that is their profile.
but i only know how to fwrite "hii this is your profile"
so all i have is a blank page.
can anyone HELP me ?

ops can someone move this i think i posted in wrong forum!!!!!!!!!

This post has been edited by emopoops: 24 May 2009 - 04:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HOW do i fwrite an entire php page?

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 05:52 PM

Moving to PHP.
Was This Post Helpful? 0
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 06:15 PM

Well.....to do it like you posted may not be the easiest way, in fact it'd be rather ugly and difficult, but something like this:

<?php
$handle = fopen('userdirectory/index.php', 'w');
$string = '<?php
echo("Hello world!");
?>';
fwrite($handle, $string);
fclose($handle);
?>


But in the $string variable you'd place whatever you wanted on their profiles...
Like I said, this isn't the best method for doing it and it'll be really ugly, but it's your wish.

Yours,
Shane~

This post has been edited by ShaneK: 24 May 2009 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 06:16 PM

If I understand what you're asking, you'd like a separate index.php file for each user, to be placed in their own user directory. That means that if you've got 1000 users, you'll have 1000 index files that are essentially identical. That's a bit wasteful. Why don't you set a user's directory to a variable and use that instead?
Was This Post Helpful? 0
  • +
  • -

#5 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 06:18 PM

Well I would rather suggest you created a template that every user would use and simply change the content depending on user! :) Why do I suggest this? Well, say that you realize you made a mistake a couple of months from now and you during this time got 100 users, then you would have to change the content within 100 pages. if you use a template you only have to change one! :)

But to really answer your question anyway, to write PHP to a file is no different than writing "Hello my name is silly", you just have to make sure you escape every "$" with an "\" else it might be executed before it's written to the file!

Hope it helps! :)


View Postemopoops, on 25 May, 2009 - 01:31 AM, said:

i just want to know the easiiest way / how to do it like a link im not asking for u to give me a script but if anyone knows how.
i have a registration script once the person registers,
the script makes a directory that is their username and then and index.php file in the directory that is their profile.
but i only know how to fwrite "hii this is your profile"
so all i have is a blank page.
can anyone HELP me ?

ops can someone move this i think i posted in wrong forum!!!!!!!!!

Was This Post Helpful? 0
  • +
  • -

#6 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 06:19 PM

Why don't you just save their data in a database and withdraw it when they view their profile? It'd all be the same page, but it'd be their own information as opposed to their entire own page.....

That'd be a lot easier, you'd think....

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#7 emopoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 08:40 PM

ok thanks for the responses first and foremost.
seriously i can write the php code but would only have to escape the $variable not the
" " quotes or anything?
is the only thing i have to escape is the variable and do it like the first person that answered replied.?

and if so, what about the template? i was thinking about using a css stylesheet to be included when the file is produced when the user first registers, but what is a template? is that something like an include like a stylesheet is but has html too?
im confused.

so idecided to try it heres what i tried:
(fwrite to a test file)
test.php:
<?php
$contents = "<?php
session_start();
require(\$_SERVER[\"DOCUMENT_ROOT\"] . \"/checkforlogin.php\");
require(\$_SERVER[\"DOCUMENT_ROOT\"] . \"/peace.php\");
\$userfinal=\$_SESSION['username']; ?>
<head><title>Profile - <?php echo \$userfinal ?></title>
<link rel=\"shortcut icon\" href=\"http://www.socialemo.com/favicon/favicon.ico\">
<link href=\"http://www.socialemo.com/style/new_message.css\" rel=\"stylesheet\" type=\"text/css\" />
</head>";
$fp = fopen('test1.php', 'w');
fwrite($fp, '$contents');
fclose($fp);
?>

this is what i got when i opened test1.php(nothing was writen but $contents i checked:
$contents


so why is it just writing $contents and not the actual contents?

This post has been edited by emopoops: 24 May 2009 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#8 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 09:27 PM

If you did it like I posted, the only things you'd have to escape are 's. (Apostrophes)

A template would be like....well, basically a general layout that's easy to modify...listen, man, you really don't wanna do it like how I posted it (how you requested it) try learning a bit more about PHP and learn how to set up a single page so that it withdraws and lays out the data of the account, instead of making several hundred pages (one for each registered user)....you could really end up regretting it if you use the method I posted.

Yours,
Shane~

This post has been edited by ShaneK: 24 May 2009 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 emopoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-May 09

Re: HOW do i fwrite an entire php page?

Posted 24 May 2009 - 09:55 PM

i dont understand another way? how do i find the way? what do i search for?ok so i got it to work the way u posted. but i had to take out the echo part. thanks! and i only had to escape the ' apostrophes/ but i still have to do the whole page/ but i dont know if there is an easier way. cause i cant find one/ :[[[[[[[

This post has been edited by emopoops: 24 May 2009 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: HOW do i fwrite an entire php page?

Posted 25 May 2009 - 05:30 AM

Try removing the " ' "s around $content when writing it to the file! I think the " ' " makes it used as a string instead of a variable! :)

A template is one single page that everyone uses. Take a look at this URL: "http://example.com/template.php?user_name=SillyGod". Here we send a GET variable to the template. With that variable we could change the content of the page, if we store the user data in a database. Are you following?

To make a complete example, template.php:
<?php
if(isset($_GET['user_name']))
{
	echo "This is ".$_GET['user_name']."'s profile!";
}
else
{
	echo "The user you are trying to find does not exist!";
}
?>

If you think that "?user_name=SillyGod" makes the URL look ugly there's a way to get around that too if you are running your site on an apache server and have the privileges to create .htaccess files and use mod_rewrite. Then you could simply change the url to "http://example.com/SillyGod" if you'd like to! :)

Hope I didn't make it even more complicated!



View Postemopoops, on 25 May, 2009 - 05:40 AM, said:

ok thanks for the responses first and foremost.
seriously i can write the php code but would only have to escape the $variable not the
" " quotes or anything?
is the only thing i have to escape is the variable and do it like the first person that answered replied.?

and if so, what about the template? i was thinking about using a css stylesheet to be included when the file is produced when the user first registers, but what is a template? is that something like an include like a stylesheet is but has html too?
im confused.

so idecided to try it heres what i tried:
(fwrite to a test file)
test.php:
<?php
$contents = "<?php
session_start();
require(\$_SERVER[\"DOCUMENT_ROOT\"] . \"/checkforlogin.php\");
require(\$_SERVER[\"DOCUMENT_ROOT\"] . \"/peace.php\");
\$userfinal=\$_SESSION['username']; ?>
<head><title>Profile - <?php echo \$userfinal ?></title>
<link rel=\"shortcut icon\" href=\"http://www.socialemo.com/favicon/favicon.ico\">
<link href=\"http://www.socialemo.com/style/new_message.css\" rel=\"stylesheet\" type=\"text/css\" />
</head>";
$fp = fopen('test1.php', 'w');
fwrite($fp, '$contents');
fclose($fp);
?>

this is what i got when i opened test1.php(nothing was writen but $contents i checked:
$contents


so why is it just writing $contents and not the actual contents?

This post has been edited by Wimpy: 25 May 2009 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#11 emopoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-May 09

Re: HOW do i fwrite an entire php page?

Posted 25 May 2009 - 10:47 PM

so if someone types in socialemo.com/theusername it will still send it to template.php?
i dont understand how to send it to template.php from the indexpage.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: HOW do i fwrite an entire php page?

Posted 26 May 2009 - 06:03 AM

Why can't the index page be the template page? PHP pages are not html pages. PHP is dynamic. That means that they can create any html you like on the fly. Don't think of php as a page. Think of it as a program.
Was This Post Helpful? 0
  • +
  • -

#13 emopoops  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-May 09

Re: HOW do i fwrite an entire php page?

Posted 26 May 2009 - 05:43 PM

View PostCTphpnwb, on 26 May, 2009 - 05:03 AM, said:

Why can't the index page be the template page? PHP pages are not html pages. PHP is dynamic. That means that they can create any html you like on the fly. Don't think of php as a page. Think of it as a program.

i thot that the template had to be included or something cause there will be multiple index pages with the same template. i just dont know how to include the template on all index files.
Was This Post Helpful? 0
  • +
  • -

#14 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: HOW do i fwrite an entire php page?

Posted 26 May 2009 - 06:11 PM

Listen, this is a lot more simple than it sounds. I'll give you an example...

Say this is our link:

Quote



So our GET variable here is user and the information stored in the variable is the string "George"

What we do next is, on users.php we have a nice little code like this:

<?php
$user = $_GET['user'];
//That sets the user to the user variable from the get data, in this case it's George
$sql = 'SELECT * FROM `myAwesomeTable` WHERE `User` = \''.$user.'\'';
$answer = mysql_query($sql);
if(mysql_num_rows($answer) == 1){
		$row = mysql_fetch_assoc($answer);
		echo("Hello ".$row['User']);
}else{
		die("This user does not exist!");
}
//This is all assuming you're using a database, if you're not then you'll want to retrieve and manage your data however you plan on doing it.
?>


Of course, if you're not using a database then you're going to have a hell of a time doing this no matter what you do.

[Note] This is just an example, do NOT use this as actual code or you could very easily end up with a lot of security holes.

[Edit/Also note] You can do this a lot of ways, with logging in and sessions or POST data....it doesn't really matter, the key concepts are the same thing. You don't want each user to have their page, but you want one page to handle each user. users.php here would handle everybody....

I hope you understand this somewhat now...

Yours,
Shane~

This post has been edited by ShaneK: 26 May 2009 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1