Log in

View Full Version : question about url



gylim78
12-29-2011, 03:57 PM
Hi all,

Got a question.
I am creating a website where different users gets their own directory. Something like facebook where you can have www.facebook.com/<user>

I am curious how is this done. Do you really create a subdirectory for every single user? I don't think that it is the case. But I can't seem to find how to get it done.

Anyone who can point me in the right direction would be greatly appreciated.

Nile
12-29-2011, 05:13 PM
It's called htaccess mod rewrite. It basically works like this (I think):

When you go to the url (i.e., domain.com/gylim), the server rewrites the url like this: domain.com/users.php?username=gylim.

It's also smart to have a list of reserved names that can't be taken like: admin, class, register, about, login, signup, site, style, script, templates, etc... (you can probably find a list of them online)

baconDelta
12-29-2011, 05:40 PM
oh jesus my browser crashed after i wrote up a nice guide for you x.x i'll try again.....

well it's kind of a big project if you're new. you'll have to create a login, write something that dynamically makes sessions and cookies, make a database of your users and hashed passwords, make a cms to edit that database as the admin, etc. but yes it's definately possible. you should use functions like fopen, fwrite to make the new page.

what you should start with is how to use the data submitted from a form. then worry about making a login and all that later. a form generally looks like this:


<form action="where_this_form_is_processed.php" method="post" enctype="multipart/form-data">
what should your url be? www.mywebsite.com/members/<input type="text" name="new_url" id="new_page_name" />
<br />
<br />
Whats your name?: <input type="text" name="name" value="" />
<br />
<input type="submit" name="submit" value="Upload" />
</form>


you can have other junk on the form but that's just a basic one for this example. anyway the form above is processed wherever 'action' says. so if this form is on 'www.mywebsite.com/registration.php' you can even have the php code on the registration.php file, and have that say action="registration.php".

what that
method="post" means is that the data submitted from this form will be posted to the 'action' page. so in your php you can grab things from the form and assign them to variables, like:


<?php
$new_page = $_POST["new_url"];
?>

so above we've grabbed what was name="new_url" in the form, and assigned it to $new_page the variable. from here we can use fopen and fwrite to make a new page with that variable. although you should have a template ready, with placeholders for more variables. if we just make a new page with the above variable and nothing else, it'll be a blank page.

how to make a template and getting it is a whole different story.


$put_newpage_here = "members/";
$page_name = $new_page.".html";
$ab = fopen($put_newpage_here.$page_name, "w");
fwrite($ab, $your_template_variable);
fclose($ab);

by the time
$your_template_variable is in the above it would need to already be put together as:


your_template_variable = str_replace($your_placeholders_as_an_array, $your_new_data_to_replace_placeholders_as_an_array, $the_variable_equal_to_the_location_of_your_template);

$the_variable_equal_to_the_location_of_your_template would be the name of the path to your template as a variable appended to the name of your variable. remember that appending in php is just the period, so it would just be:


$template = "newmember.html";
$template_path = "the_folder_template_is_in/";
$the_variable_equal_to_the_contents_of_your_template = file_get_contents($template_path.$template);

this would have to be executed before fwrite is.
this is what i meant by a whole different story.

anyway your new file would be at mywebsite.com/members/($page_name) after all that. just makes sure the placeholders line up well with the array of new info, then you won't get an empty page but a page full of that template with the right data in it.

hope that wasn't too confusing, but that should get you started.

baconDelta
12-30-2011, 05:38 AM
what nile mentioned would change the url in case the member pages are stored in a database. i'm always iffy about storing entire pages in databases since i think it'd slow things down once the table gets large enough.

but whatever floats your boat!

gylim78
12-31-2011, 05:15 AM
Thanks to both Nile and baconDelta for your advice.

I think htacess mod rewrite is what I was looking for ... Thanks Nile

baconDelta : Thanks for writing the lengthy post. I appreciate it. I have actually done all the steps that you said. My website is up and running. The only thing is that I am actually creating an actual directory for each user which is increasing the number of directories in the site. And I need some way to manage that.

baconDelta
12-31-2011, 06:14 PM
aw man i misunderstood then x.x

mod rewrite would def make it easier to manage a large base of users, but let me know if you notice sluggishness when your table gets big. i'm curious, because i've heard a table over 500mb can get slow if lots of users are accessing it. that may be old news idk. enlighten me :D

i would just have them make their own directory in members/ with mkdir() and just save the url and other short attributes in a db. i'm curious as to what the main issue is with doing it this way?