How to set a custom Profile URL for my user? - php

you all know that if you login to your Facebook or Twitter account, you see a URL on address bar like this: https://web.facebook.com/iamakm15. That means your username appears on URL. My question is, how Facebook and Twitter did this? I mean what programming language did they use to show the username in the URL?
Now, when I login to my profile, I get a URL like this: http://localhost/AKM.PHP/project_akm/profile.php, so how can I add username after profile.php like this: /profile.php/imakm15 to my URL?

Later later edit ( didn't have space for full explanation in comment)
Ok, good. Let's start with the beginning. When you don't have rewrite setup on yoru server, your link will look like this:
http://localhost/AKM.PHP/project_akm/profile.php?username=iamakm15
Profile.php will extract the parameter with $_GET['username'] in your php code, it will make a query in database, where clause username='iamakm15' and it will extract the info associated with 'iamakm15'. If this step is already done and it works, in your root localhost ( you need to place this file at this level:localhost in order to work), add a file called '.htaccess' - if you already have one, just add these lines:
RewriteEngine On
RewriteBase /
RewriteRule ^AKM.PHP/Project_akm/([^/\.]+)/?$ /AKM.PHP/Project_akm/profile.php?username=$1 [L]
After saving this file, just try to access url:
http://localhost/AKM.PM/project_akm/iamakm15
and you should see the page with info about user: iamakm15. Make this exercise with different usernames from database, by modifying iamakm15 with other one.
if you get a 500 error you can add a new rule ( after RewriteBase / ):
RewriteRule ^/AKM.PHP/Project_akm/profile\.php$ - [L]
because can match itself after redirect.

Related

How can there be a user directory in the URI?

Apologies if this is a repeat, I really did not know what to search for this.
How can there be a user directory in the URI without creating a file for each user (which surely would be inefficient)? Or is that what is actually happening?
For example, youtube has "youtube.com/user/[username]" and it would take you to that specific profile. It seems similar to the GET request method (eg "?Profile=[username]"). Is this accomplished via PHP/.htaccess? If so then please tell me how to do via PHP/.htacess for my own use. If not, then I would still like to know the theory of how it is done.
Many thanks!
Jon
You can use apache mod_rewrite, i.e.:
Create an .htaccess file on the root of your website with the following content:
RewriteEngine on
RewriteRule ^user/(.*)$ /user.php?user=$1 [NC]
The above will redirect any request to http://yoursite.com/user/janedoe to user.php. To get the value on user.php, you can use:
<?php
if(!EMPTY($_GET['user'])){
$username = $_GET['user'];
//janedoe
}
?>
now, you can use the value of $username to, for example, query a database to get specific user details.

Creating dynamic file in php (not the title)

What I want to do:
Create dynamic php file for each product. What I'm currently doing is, if a person clicks on a products , it opens in one single file with some data i.e. www.example.com/showproduct.php?ref=1085. Here 1085 is a productid & thus it shows the content from DB for the particular id.
Is it possible to show the URL as www.example.com/google-nexus-5-16gb-black.php, where this data comes from DB content?
I don't want to create a page for each product, I think that would be a waste. Is there any solution? You can see even on stackoverflow how it shows the URL for each question.
EDIT
I've used following code in .htaccess file, however it's not rewriting the URL.
RewriteEngine on
RewriteRule ^ref/([A-Za-z0-9-]+)/?$ showproduct.php?ref=$1 [NC]
Any problem with this? And my I will try connecting to DB & get the file name once I know how this works.
You can create a unique alias field for each product record in your DB, and then you can use mod_rewrite to map www.example.com/YOUR-ALIAS to www.example.com/showproduct.php?alias=YOUR-ALIAS. This will give your the desired friendly URL effect.
Update
In your case it appears that you merely want to map google-nexus-5-16gb-black.php to showproduct.php?ref=1085, creating a additional alias field may be overkilled. In this case, you may setup a single rewrite rule:
RewriteEngine on
RewriteRule ^google-nexus-5-16gb-black.php$ /showproduct.php?ref=1085 [L]
.htaccess
URL rewriting file. Redirecting original URL www.example.com/showproduct.php?ref=1085 to www.example.com/google-nexus-5-16gb-black.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).php$ showproduct.php?ref=$1
RewriteRule ^([a-zA-Z0-9-/]+).php/$ showproduct.php?ref=$1
You can read more about Basic URL Rewrite
<?php
include('db.php'); // Connection to your database
if($_GET['ref'])
{
$url=mysql_real_escape_string($_GET['ref']);
$sql=mysql_query("select title from table where ref='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
}
?>
Probably this link will help you to do what you are looking for

php query without var=

I'd like the end user of my website to be able to do something along the lines of: example.com/user/user_name to show the statistics of that user on a different website. In short, I'd like to be able to make queries in php without having the user do: example.com/user/username=user_name I also have no way of knowing all of the possible users. I guess this means I'd like to always redirect to the index, while preserving the querystring that doesn't have "var=" in it. How would I do this?
you cna use the explode function in php. use it on the url slashes "/" and you get valid data to query. Remember to make it secure from sql injections.
EDIT
here you go:
$url = explode('/',$_SERVER['REQUEST_URI']);
echo 'user = '.$url[count($url)-1].'<br />
username = '.$url[count($url)-2];
EDIT 2
yeah you need to use .htaccess
unless you wanna get away with the questionmark
http://example.com?/user/username
EDIT 3
if you use apache you can make all traffic go through index.php with following .htaccess file:
RewriteEngine On
# always run through the indexfile
RewriteRule .$ index.php
Then if you want some folder to not go through to index.php, you add a htaccess file in that folder with:
RewriteEngine Off
The easiest way to do this is to still have the script be somepage.php?username=user_name and then have in your .httaccess file:
RewriteEngine On
RewriteRule ^/user/(.*) somepage.php?username=$1
This way you can still get the variable with $_GET['username'] but it looks nice to the user.

mod_rewrite php mysql

I'm really new at mod_rewrite and i have been trying to figure this out but really stuck. p
Here is my issue.
I have a page http://example.com/user/?s=81
?s=81 is reading from user id in the db.
What i would like to have is a link
http://example.com/nookie
In the database i have a field called whatuser
so on row 81 in that field i have user nookie
So what i would like is to read from databse what user it is in database
and create easi url from it.
i have also several php pages inside that user folder so i need to be able to link
to them like
example.com/nookie/step1.php
example.com/nookie/step2.php
To my knowledge you can not query databases with mod_rewrite.
how about putting a PHP-script to /user/?s=81, that looks up the user's name in the db and then relocates the user to $url = "/$username"; see PHP's header function passing "Location: $url" to it.
The other possibility (that I haven't thought through too much :) ) is that you add an extra field to the database e.g. user_home = /nookie/. So the logon script grabs it when verifying account details and thats where you send them to on successful validation? No rewrites /extra scripts needed.
Here's what I would suggest: if your username is only alphanumeric chars, you could pass the nick to all your Php files, after an internal rewrite, in the GET query.
Something like:
RewriteRule /([a-zA-Z0-9]+)$ /user/?s=$1 [QSA,L]
RewriteRule /([a-zA-Z0-9]+)/(.*)$ /$2?s=$1 [QSA,L]
And then, just have the "mother of mother" classes in Php that check for a value "s" in the query string ($_GET) which checks in the database if the user exists. If the user doesn't exists, make a 404 in Php.
I have almost sorted it out and here is the solution so far:
Here is the .htaccess row
RewriteRule ^([a-z0-9_-]+)$ user/?s=$1 [L,NC]
Then in the index file i have the following:
$trafikskola_id = mysql_real_escape_string($_GET['s']);
$vilken_trafikskola = mysql_query("SELECT * FROM users where slug='$trafikskola_id'") or exit(mysql_error());
In the database i have create slug field and inside that on row 81 added nookie and my url looks like
http://mypage.com/nookie instead of http://mypage.com/user/?s=81 =))
However i have the issue that i can not have link
http://mypage.com/nookie/ to have a forwardslash
And aswell non of my css - js - images are working.. Any ideas to solve those issues?

How To rewrite a url with PHP?

Let' say for example one of my members is to http://www.example.com/members/893674.php. How do I let them customize there url so it can be for example, http://www.example.com/myname
I guess what I want is for my members to have there own customized url. Is there a better way to do this by reorganizing my files.
You could use a Front Controller, it's a common solution for making custom URLs and is used in all languages, not just PHP. Here's a guide: http://www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html
Essentially you would create an index.php file that is called for every URL, its job is to parse the URL and determine which code to run base on the URL's contents. So, on your site your URLs would be something like: http://www.example.com/index.php/myname or http://www.example.com/index.php/about-us or http://www.example.com/index.php/contact-us and so on. index.php is called for ALL URLs.
You can remove index.php from the URL using mod_rewrite, see here: http://www.wil-linssen.com/expressionengine-removing-indexphp/
Add a re-write rule to point everything to index.php. Then inside of your index.php, parse the url and grab myname. Lookup a path to myname in somekinda table and include that path
RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]
index.php:
$myname = $_SERVER['REQUEST_URI'];
$myname = ltrim($myname, '/'); //strip leading slash if need be.
$realpath = LookupNameToPath($myname);
include($realpath);
create a new file and change it name to (.htaccess) and put this apache commands (just for example) into it :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^profile/([0-9]*)$ members.php?id=$1
You must create a rewrite rule that point from http://www.example.com/myname to something like http://www.example.com/user.php?uname=myname.
In '.htaccess':
RewriteEngine on
RewriteRule ^/(.*)$ /user.php?uname=$1
# SourceURL TargetURL
Then you create a 'user.php', that load user information from 'uname' GET variable.
See from your question, you may already have user page based on user id (i.e., '893674.php') so you make redirect it there.
But I do not suggest it as redirect will change the URL on the location bar.
Another way (if you already have '893674.php') is to include it.
The best way though, is to show the information of the user (or whatever you do with it) right in that page.
For example:
<?phg
vat $UName = $_GET['uname'];
var $User = new User($UName);
$User->showInfo();
?>
you need apache’s mod_rewrite for that. with php alone you won’t have any luck.
see: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Categories