Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Nice Profile URL?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #36453
    schart
    Participant

    I put this in the PHP section, but it might as well be .htaccess . So I have this basic profile page, kind of like facebook’s:
    /profile.php?id=1

    By using PHP, I want to get the username for that ID (which I know how to do), and then go like this:

    Redirect
    /profile.php?id=1
    to go to
    /Schart

    My “get username from id” code:



    $id = $_GET; //ID of page, (profile.php?id=1)


    /* Also, a session with user information:
    $username = $_SESSION; // Username of logged in user
    $password = $_SESSION; // Password of logged in user
    */

    $write = mysql_query("SELECT username FROM husers
    WHERE id = '$id'");
    $uname = "";
    while ($row = mysql_fetch_assoc($write)) {
    $uname .= $row; // Username of the profile page.
    }
    ?>

    http://underbakke.net/profile.php?id=1
    http://underbakke.net/index.php

    #95914
    bungle
    Member

    Expanding on the code i gave you in the other thread at https://css-tricks.com/forums/discussion/15865/if-id-does-not-exists-php#Item_5

    PHP can do the redirect for you with a header rewrite – but, and this is important, the header rewrite only works if you have not yet outputted anything else so this PHP code needs to come at the very top of your php file with no whitespace before it

    
    $id  = mysql_real_escape_string($_GET);
    $sql = "SELECT * FROM users WHERE id=$id";
    $result = mysql_query($sql);

    if (mysql_num_rows($result)==0) {
    echo ('User never existed');
    }
    else if (mysql_num_rows($result)==1) {
    $userinfo = mysql_fetch_array($result);
    if (!$userinfo) {
    echo ('User no longer exists.');
    }
    else {
    $username = $userinfo;
    header('Location: http://www.yoursite.com/'.$username);
    }
    }
    ?>
    #95920
    bungle
    Member

    oh i see you want the url to change and then it to pull the correct page then you would need some .htaccess rules to make that work else each username will need a page

    #95928
    bungle
    Member
    #95929
    bungle
    Member

    And the original code would need to be expanded to account for username variables too so

    oh i see you want the url to change and then it to pull the correct page. Then you need to do something like the following

    
    
    if (isset($_GET)) {

    $id = mysql_real_escape_string($_GET);
    $sql = "SELECT * FROM users WHERE id=$id";
    $result = mysql_query($sql);

    if (mysql_num_rows($result)==0) {
    echo ('User never existed');
    }
    else if (mysql_num_rows($result)==1) {
    $userinfo = mysql_fetch_array($result);
    if (!$userinfo) {
    echo ('User no longer exists.');
    }
    else {
    $username = $userinfo;
    header('Location: http://www.yoursite.com/'.$username);
    }
    }
    }

    else if (isset($_GET)) {

    $uname = mysql_real_escape_string($_GET);
    $sql = "SELECT * FROM users WHERE username=$username";
    $result = mysql_query($sql);

    if (mysql_num_rows($result)==0) {
    echo ('User never existed');
    }
    else if (mysql_num_rows($result)==1) {
    $userinfo = mysql_fetch_array($result);
    if (!$userinfo) {
    echo ('User no longer exists.');
    }
    else {
    echo('Welcome to your Profile Page '.$userinfo['full name');
    }
    }
    }
    ?>
    #95947
    bungle
    Member

    Sorry if i confused you with my earlier suggestion, I was suggesting you’d need a combination of both mod rewrite and the php. You’d obviously need to make sure the mod rewrite was selective enough not to catch the full redirected URL that php needs also and the header rewrite would need to be redone to avoid the double redirect.

    On a different thought, if you were doing ajax you could alternately use a hashtag like “site#username” and then pull the pages with a get request to PHP but this would only work if you were using ajax as hashtags don’t get passed server side to PHP.

    #95949

    @schart : I would’ve been glad to know this site when I started :D


    @bungle
    : I’d suggest rather not to use Ajax with hashtags until history.pushState is widely supported to use Ajax and give parameters to Php on reload. It’s also the way Facebook and Github do it, Facebook just also provides it with hashtags for older browsers

    #95951
    bungle
    Member

    you can have the php run the queries from either id or username anyway

    so “site/profile/username” becomes rewritten to site/profile.php?u=username”

    and the php then does

    $username=mysql_real_escape_string($_GET);
    “SELECT * from users where username=$username”

    #95952

    Why you don’t just deliver the name instead of the Id to the profile.php?

    #95953

    Bungle was faster than me, because of my phone, this German autocorrect is killimg me!

    #95955

    “select Id from Table where Name=’$name'” and you have the id

    #95956

    I don’t think you can set the handler of .htaccess because that is done in .htaccess. There’s no way of using Php within .htaccess

    #95969
    bungle
    Member

    “Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /customers/9/7/5/underbakke.net/httpd.www/profile.php on line 25 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /customers/9/7/5/underbakke.net/httpd.www/profile.php on line 92”

    Your mysql_query() must be returning false rather than an array.

    I would guess that is because your query needs to query username = “something” where as id can successfully be queried against a non quoted integer like id = 3

    you would need to modify the query to

    ‘SELECT * from users WHERE username = “‘.$username.'”‘

    so that the username is quoted out in the actual query string.

    #95997
    bungle
    Member

    Something else must be wrong in the query though, that error means the query is failing

    I would put an echo line in after the query to see exactly what the query that php is running is. You can then run that query manually and have sql tell you what’s wring with it.

    $sql=’SELECT from blah blah’;
    echo $sql;
    $result = mysql_query($sql);

    #96006
    bungle
    Member

    Couple of different points for you

    In the middle of this you have a series of queries run one after the other using a while loop to and mysql_fetch_assoc each time to fill a variable

    You can fill all of these with a single query and mysql_fetch_array()

    So instead

    $result = mysql_query(“SELECT info,email,img FROM husers WHERE id = $id”);
    $userinfo = mysql_fetch_array($result);
    $info = $userinfo;
    $email = $userinfo;
    $img = $userinfo;

    will save you running multiple queries

    Also, be careful when including variables in query statements

    PHP will substitute in variables only inside double quotes

    So $sql = ‘SELECT * from users where id = $id’;

    will not work, instead you need

    $sql = “SELECT * from users where id = $id”;

    or

    $sql = ‘SELECT * from users where id = ‘.$id;

    and if you need to include a string rather than an integer then you need to quote that string inside the sql statement

    so $sql = ‘SELECT * from users where username = “‘.$uname.'”‘;

    will result in an actual query of SELECT * from users where username = “john” or whatever

    Make sense?

Viewing 15 posts - 1 through 15 (of 21 total)
  • The forum ‘Back End’ is closed to new topics and replies.