0

So I am wanting to allow my members to view a profile via a url such as: mywebsite.com/account/Username

however, at the moment my members can view via the url: mywebsite.com/account?username=username.

This doesn't look profesional and I've tried nearly everything to get it to the url I'm looking to get. (Please be aware; I'm very new to this website and cannot use it properly, If I have done anything wrong, please notify me and I will justify it.) The code:

//get config
$config = $base->loadConfig();

full code: https://pastebin.com/UmAmF9Rt

    <?php 
require('../includes/config.php');
require('../structure/base.php');
require('../structure/forum.php');
require('../structure/forum.index.php');
require('../structure/forum.thread.php');
require('../structure/forum.post.php');
require('../structure/database.php');
require('../structure/user.php');

$database = new database($db_host, $db_name, $db_user, $db_password);
$base = new base($database);
$user = new user($database);
$forum = new forum($database);
$forum_index = new forum_index($database);
$thread = new thread($database);
$post = new post($database);
$user->updateLastActive();

//get config
$config = $base->loadConfig();

//set some variables that are used a lot throughout the page

if (!empty($_GET['username'])) {
    $profile_name = htmlspecialchars($_GET["username"]);
}
else{
    $profile_name = $user->getUsername($_COOKIE['user'], 2);
}
$username = $user->getUsername($_COOKIE['user'], 2);
$rank = $user->getRank($username);

$f = $_GET['forum'];
$i = $_GET['id'];


//assign data to details[] array
$details['lock']        = $detail_query[0]['lock'];
$details['sticky']      = $detail_query[0]['sticky'];
$details['title']       = stripslashes(htmlentities($detail_query[0]['title']));
$details['username']    = $detail_query[0]['username'];
$details['status']      = $detail_query[0]['status'];
$details['content']     = $detail_query[0]['content'];
$details['date']        = $detail_query[0]['date'];
$details['lastedit']    = $detail_query[0]['lastedit'];
$details['qfc']         = $detail_query[0]['qfc'];
$details['moved']       = $detail_query[0]['moved'];
$details['hidden']      = $detail_query[0]['hidden'];
$details['autohiding']  = $detail_query[0]['autohiding'];               

//get forum details
$forum_details = $database->processQuery("SELECT `title` FROM `forums` WHERE `id` = ?", array($f), true);



if(isset($_GET['username'])){
    if($user->doesExist($_GET['username'])){;
    }
}else{
    if(!$user->isLoggedIn()){
        $base->redirect('../login.php');
    }else{
        $user_s = $username;
    }
}
$messages = array();
$avatar = $user->getAvatar($profile_user);
$usr = $user->getUsername($profile_user);

if($username == $profile_user && $user->isLoggedIn() && isset($_REQUEST['cust_title'])) {
    $user->setTitle($username, htmlentities($_REQUEST['cust_title']));
}
if($user_s == $username && $user->isLoggedIn() && isset($_FILES['uploaded'])) {
    if(isset($_REQUEST['delete'])) {
        $user->setAvatar($username, '');
        $messages[] = "Your avatar has been removed.";
    } else {
        $ok = false;
        $info = getimagesize($_FILES['uploaded']['tmp_name']);
        if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
            $messages[] = ("Upload failed with error code " . $_FILES['uploaded']['error']);
        } else if($info === FALSE) {
            $messages[] = ("Unable to determine image type of uploaded file");
        } else if(($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
            $messages[] = ("Not a gif/jpeg/png");
        } else if($_FILES['uploaded']['size'] > 350000) {
            $messages[] =  "Your file is too large.";
        } else if($_FILES['uploaded']['type'] == "text/php") {
            $messages[] =  "No PHP files";
        } else {
            $ok = true;
        }
        $target = md5(strtolower(trim($username))) .'.'. pathinfo($_FILES['uploaded']['name'])['extension'];
        if($ok) {
            if(move_uploaded_file($_FILES['uploaded']['tmp_name'], "../images/avatar/" . $target)){
                $messages[] =  "Your avatar has been uploaded. Please allow atleast 10 minutes for it to update.";
                $user->setAvatar($username, $target);
            } else {
                $messages[] =  "Sorry, there was a problem uploading your file.";
            }
        }
    }
}

//retrieve posts/threads
$posts = $database->processQuery("SELECT `id`,`thread`,`username`,`timestamp`,`content` FROM `posts` WHERE `username` = ? AND ". time() ." - `timestamp` < 1209600 ORDER BY `id` DESC", array($user_s), true);
$threads = $database->processQuery("SELECT `id`,`parent`,`title`,`username`,`timestamp`,`content` FROM `threads` WHERE `username` = ? AND ". time() ." - `timestamp` < 1209600 ORDER BY `id` DESC", array($user_s), true);

//type:id:forum:timestamp:(if post)thread
$list = array();

foreach($posts as $post){

    //get the thread's forum/parent
    $t = $database->processQuery("SELECT `parent` FROM `threads` WHERE `id` = ? LIMIT 1", array($post['thread']), true);

    $list[$post['timestamp']] = 'p:'.$post['id'].':'. $t[0]['parent'] .':'.$post['timestamp'].':'.$post['thread'].':'.$post['content'];
}

//add threads
foreach($threads as $thread){
    $list[$thread['timestamp']] = 't:'.$thread['id'].':'.$thread['parent'].':'.$thread['timestamp'].':'.$thread['content'];
}

//now sort them
krsort($list, SORT_NUMERIC);

$r = $database->processQuery("SELECT * FROM `users` WHERE `username` = ?", array($profile_name), true);
?>
SANM2009
  • 1,918
  • 2
  • 12
  • 30
Matt Bra
  • 3
  • 2
  • Possible duplicate of [How to create friendly URL in php?](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) – M. Eriksson Jan 01 '18 at 01:27

1 Answers1

0

Your best bet is to use: .htaccess route with mod_rewrite

Try Adding a file called .htaccess in your root folder, and add something like this:

  RewriteEngine on
  RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /account.php?username=$username

This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want:

Refer to this answer by Niels Keurentjes: https://stackoverflow.com/a/16389034/3367509

If you are new to .htaccess look up this question: What is .htaccess file?

emekamba
  • 188
  • 9