Results 1 to 6 of 6

Thread: question about url

  1. #1
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default question about url

    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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)

  3. The Following User Says Thank You to Nile For This Useful Post:

    gylim78 (12-31-2011)

  4. #3
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    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:
    Code:
    <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
    Code:
    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:
    Code:
    <?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.
    Code:
    $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
    Code:
    $your_template_variable
    is in the above it would need to already be put together as:
    Code:
    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:
    Code:
    $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.

  5. The Following User Says Thank You to baconDelta For This Useful Post:

    gylim78 (12-31-2011)

  6. #4
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    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!

  7. #5
    Join Date
    Dec 2008
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #6
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    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

    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •