How could I create profile url's similar to what myspace has?

Discussion in 'PHP' started by Crayz, Oct 27, 2008.

  1. #1
    Hello, I'm not sure whether this should have been in the .htaccess forum, or in here.

    Basically, I'm trying to create url's similar to the way myspace does it.

    Say a users profile is: /viewprofile.php?userid=56
    And that users username is: Testaccount

    I'm looking for a way to be able to redirect www.mysite.com/testaccount to www.mysite.com/viewprofile.php?userid=56

    If anyone has a solution, it would be appreciated much!
    Thanks!
     
    Crayz, Oct 27, 2008 IP
  2. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #2
    This is done by using switches in php, an if and else statement may work as well but a switch is more efficient.
     
    ryandanielt, Oct 27, 2008 IP
    Crayz likes this.
  3. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #3
    Edit:
    I managed to get something working.

    It involves using the 404 error page, though, which I'm not sure how it will affect the SEO of my website.

    Basically, I have my 404 error page using a script such as:
    
    include('../_inc/connect.php');
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    
    $url = curPageURL();
    $userurl = explode("site.com/", $url);
    
    $getId = mysql_query("SELECT * FROM SK_users WHERE username='$userurl[1]'");
    $row = mysql_fetch_array($getId);
    	$id = $row['id'];
    mysql_close();
    
    Header('location: ../viewprofile.php?userid='.$id.'');
    
    PHP:
    When a person types something such as:
    www.mysite.com/Username

    It will search for the username 'Username' in the database, grab it's ID, and redirect properly.
    I should be able to come up with a way to separate error pages from user profiles, with a bit of algorithm & coding.

    Any suggestions from anyone?
    Thanks!
     
    Crayz, Oct 27, 2008 IP
  4. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you're interested in doing this an even better way (without the need for redirects, so your URL stays/looks just "www.mysite.com/testaccount" and works just like "www.mysite.com/viewprofile.php?userid=56" would):

    Take a look at mod_rewrite. No messy php required, does all the magic.
     
    keyaa, Oct 27, 2008 IP
    Crayz likes this.
  5. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #5
    Would this allow me to access the MySQL database & find the correct userid to redirect to?

    Thanks!
     
    Crayz, Oct 27, 2008 IP
  6. joxtechnology

    joxtechnology Peon

    Messages:
    146
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    keyaa is correct just use the mod_rewrite in .htacess file to do this
     
    joxtechnology, Oct 27, 2008 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    ads2help, Oct 27, 2008 IP
    Crayz likes this.
  8. Calon

    Calon Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    MOD rewrite is an apache tool..

    www.yoursite.com/56

    would be the it, unless you changed the id too name or whatever.
     
    Calon, Oct 27, 2008 IP