Special profile page link like www.domain.com/username - php

Hey guys I have a question. I wanna create a profile page for each new user, and I noticed that on facebook you could simply type in www.facebook.com/username and you get to the user's page, my question is, how can I do this without something like domain.com/users.php?useraname="username" or something like that? How can I simply make it like the facebook one?

What you are looking for is mod_rewrite. This will allow you to write PHP code that appears to the end user to be a directory on the server (such as www.facebook.com/user.php?username into www.facebook.com/username.)
An introduction to them with PHP can be found here: http://wettone.com/code/clean-urls
Please note you will need to enable it on your server. That should be possible in the .htaccess file if you're running an Apache server.

This is not a complete answer since I'm NOT a php guy
What you're looking for a RESTful urls, mostly you can get urls like that on your web app if you use a framework that supports restful urls
See this SO question:
REST-style URLS and PHP
See this article:
http://blog.garethj.com/2009/02/building-a-restful-web-application-with-php/
Search google and Search SO with google

This can’t be done with PHP alone. It’s the web server that needs to know how to handle these kind of request first.
Because, to put it simply, a web server just takes the requested and tries to map it onto a file in the file system below the document root directory. And if it can’t find an appropriate file, it returns an 404 error code.
Now there is some kind of URL rewriting mechanism for almost every web server software. In case of Apache as the most popular web server software out there, there is mod_rewrite that allows URL rewriting based on rules. In this case the following could enable /users.php?username=username being also accessible through /username:
RewriteEngin on
RewriteRule ^[a-z]+$ index.php?username=$0

Options +FollowSymLinks
RewriteEngine On
RewriteBase /php/profile
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(.)$ ./profile.php
http://exapmle.com/users/waqar.alamgir
in $_SERVER['REQUEST_URI'] you will see users/waqar.alamgir

Related

How to create dynamic webpage with custom name?

I have looked around and attempted my own research on this topic but to no avail just yet.
I have a dynamic webpage set up to look for a ID from a database to retrieve elements required. This results in of course the web page looking like www.site.com/page?id=1
My desired outcome would be like a title for this page to be called.
Such as say I had a fruit product it and user went to my site and went to the address /fruit it would it would be the content of ?id=1 just as an example.
I have seen this used on many a site but not sure how this is programmed or works. Is this something to do with a htaccess document?
Thanks in advance. Appreciate all the help.
While this has been asked and answered many times, I know many people find it difficult to search for this since there are so many common "noise" words related to it. For that reason, I believe it's worth answering again.
If you're using Apache as your webserver (which I'm assuming you are since you mention .htaccess), what you're looking for to create those "clean URLs" is mod_rewrite, which takes a set of rules and rewrites the URL requested by the browser to another path or script.
You would typically enable this in your Apache config or in .htaccess, and in a simple form (a one-to-one mapping) at it would look something like this (provided mod_rewrite is installed):
RewriteEngine On
RewriteRule ^fruit$ index.php?type=1 [L]
Now obviously that doesn't scale well if you have a bunch of dynamic pages you want to create, so what you can do is tell all pages that aren't a really file or directory to be passed to a file for processing, like so:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
In this case we're rewriting any request that doesn't resolve to a real file or directory to index.php, and then using the "last" flag [L] to stop processing other rules. Then in our PHP script, we can access the virtual path (in this case /fruit) by using $_SERVER['PATH_INFO'] and doing whatever conditional logic we want with that. If you don't get anything in that variable, ensure that the AcceptPathInfo On directive is set in your Apache config or .htaccess.
A way to test the basic concept/logic without having any rewrite rules would be to use a URL like https://example.com/index.php/fruit. You'll then see that in index.php $_SERVER['PATH_INFO'] will contain the string /fruit. You can rewrite URLs to files in other directories, chain rewrite rules, redirect the browser to other URLs, or even edit environment variables.
There are many good tutorials around using mod_rewrite for clean URLs, so I won't attempt to cover all the nuances here. Just know that it's a very powerful tool, but it's also pretty easy to break your rules if you aren't very comfortable with regular expressions or get lost in the many rules that are commonly in a configuration.
Note that if this is an existing site, you'll also want to use mod_rewrite or mod_redirect to redirect the old URLs to the new ones so they don't break (and for the benefit of having a single URL for search rankings).

URL variable sending and receiving

How does instagram.com pass the username variable like "instagram.com/username" or like
instagram.com/floydmayweather
without using the $_GET function and it does not turn out looking like this
instagram/index.php?username=floydmayweather
Use a URL rewrite command in your HTTP server. There are many examples out there for both Apache and nginx.
The rewrite rule happens at the server level before it hits your code. This means the URL doesn't actually have to get modified before your code receives it.
The way I do it is I configure Apache/nginx to send all URLs that do not match an existing file (so that static files like images, js and css still work) to my index.php file. Then in the index.php file I parse the URL to determine what page type to load and what data.
In your example, they would grab the last token off the URL, know that it would be a user's name in URL format, look up that user in the database and build the page accordingly.
This is where something like a front controller or URL router comes in to play in most frameworks. In index.php I would map each URL, based on its components, to a class that would then handle the actual page building.
Here is more info on the rewrite modules;
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://wiki.nginx.org/HttpRewriteModule
Some quick Googling will show you many examples for how to configure this.
Your index.php file can examine the $_SERVER array to determine the URL that has been requested. In this situation, the explode() function is your friend, for parsing the URL and checking its components :)
The Rewrite engine will be a perfect solution, for example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Rewrite engine - A rewrite engine is software located in a Web application framework running on a Web server that modifies a web URL's appearance. This modification is called URL rewriting. Rewritten URLs (sometimes known as short, fancy URLs, search engine friendly - SEF URLs, or slugs) are used to provide shorter and more relevant-looking links to web pages. The technique adds a layer of abstraction between the files used to generate a web page and the URL that is presented to the outside world.
Usage
Instead getting URL with extenstion link (.php / html etc..)
www.stackoverflow.com/index.php
You will get URL Without extenstion
www.stackoverflow.com/index

redirect facebook app to canvas url if host url is hit directly

I have iframe based facebook app, I just want to do is that whenever someone hits the application url directly http://mysite.com must be redirect to my facebook canvas url for this app say http://apps.facebook.com/mysite. This seems pretty easy but unfortunately its not clicking in my mind
any help would be appreciated
EDIT
the application is in codeigniter
EDIT
htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
php_value error_reporting 7
php_flag display_errors On
php_value auto_prepend_file prepend.php
say my domain is
http://abc.com/mysitefolder
my facebook app link is
http://apps.facebook.com/myappname
please consider http and https conversion too
also guyz one suggestion too as you see am using prepend in htaccess its just to get the user timezone for some date time stuff, is this fine am using it this way? the file actually sets a cookie for a user on very first visit to the site per session
Best Regards
Junaid
Any canvas page for an app that comes from Facebook will have $_REQUEST['signed_request'] defined. You could check for the existence of this request variable and if it is not set, redirect to Facebook.
well as far as i know there isn't any server side coding to do so... then again how could there be since it's all happening on the frontside of things what you could do is using javascript like so
if(window!=window.top){i am iframed now redirect or w/e you wanna do}
Since you want to redirect the domain name to the app, not the other way around, the fact that the app is in an iFrame isn't relevant.
The best way to do it is using .htaccess, in my opinion. This means that you can use custom URLs such as mydomainname.com/mypagename/, which would redirect to apps.facebook.com/myappname/mypagename/, so you can advertise your app easier. Another benefit is that you don't have to rely on Javascript. Some people (very small amount) have it turned off, but why parse all the PHP code to do it?
So, what you can do is have the root domain "blank" and only include the following in your .htaccess file:
RewriteEngine on
RewriteRule ^$ http://www.mydomainname.com/ [R=301,L,QSA]
RewriteRule ^/(.*) http://www.mydomainname.com/$1 [R=301,L,QSA]
QSA will also attach query parameters to the destination URL, such as "?ref=email".
Of course, if you keep the app code on the same domain, in the root, you don't really need to do anything, just tweak your authentification code.
If this doesn't work for you, please give us more details:
Where does the app "live"
If the app sits in the mydomainname.com, what happens when you access the URL directly?
How do you do the authentification?
Code helps, obviously.
Unless mydomainname.com isn't specific for the app, I'd recommend hosting the actual app on a subdomain. Actually, regardless, should help you organize. Or you might want to make the app accesibile as a website as well, with a Facebook login option.
Cheers

How to setup .htaccess to show 404 for unallowed urls?

I noticed in Drupal if you add .php to the url bar of any page it gives you a 404 message; clean urls enabled. The page is obviously a .php, but the .htaccess is preventing the user from being able to tamper with url extensions in the url bar. How could you do this using .htaccess. I have file extensions omitted at the moment, but would also like to add that feature. Thank you.
Also, this question does not pertain to Drupal. I only mentioned Drupal for and example.
Just because a file contains PHP code it doesn't mean it has to have the .php extension; even more so when you're accessing a file over the internet.
When you request http://mysite.com/page and you're using an .htaccess like Drupal's, the request is forwarded onto index.php?q=page whereupon Drupal will check it's database for a path matching page. If it finds one it will display the content for that page, if not it will (rightly) give a 404.
If you want all of your pages to be accessible with a PHP extension you could add an extra rule in your .htaccess file to remove .php from any request where the PHP file doesn't physically exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php $1 [NC]
Bear in mind though that this adds zero extra value for your site's visitors (in fact they have to remember a file extension as well as the path to the page), and it exposes exactly what server-side technology you're using so a potential attacker would have some of his work done for him.
Hope that helps.
Could you please explain that in more depth. How can it redirect content into an existing page? Is that common practice / typical way of doing things?
Yes it is a very common practice, used by most frameworks and CMS.
The principle is simple: you setup your .htaccess so that every request which doesn't match a real file or directory will be redirected to a front controller, usually the index.php in the root directory of the application. That front controller handles the request by analyzing the URL and calling the necessary actions.
In this way you can minimize the rewrite rules to just one, and you can offer customized 404 pages.
I dunno Drupal but in the usual php app every request being routed to the front controller which performs some validations and throws 404 on errors.
easy-peasy

Directly adding username to URL PHP

I would to know how one is able to append a username directly to a site url without having to put it within a query?
Eg
www.myspace.com/micheal
instead of
www.myspace.com?name=micheal
Without having to create a new folder for the user so that when the url is typed including the name, the surfer is taken directly to the user's profile.
Thanx
If you're using Apache, which, using PHP, you most likely are, look into mod_rewrite. This lets you do things like this, where www.myspace.com/micheal would be translated internally to www.myspace.com/?name=micheal before being sent to the scripts.
Take a look here http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html for the documentation on how to use it.
For the Apache web-server .htaccess file with the following code will do the thing.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?name=$1 [QSA,L]
This is called url rewriting, and is handled by mod_rewrite on Apache servers.
A rewrite rule takes the incoming uri, parses it and rebuilds it into what the script needs to run.
A very simple example:
RewriteRule ^michael$ /?name=michael$
There's lots on Google when you know where to look. Start here:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
As everyone has pointed out you want URL Rewriting.
If you are using IIS rather than Apache, there are still a couple of options.
Free Option - Ionics Isapi rewrite filter
Commercial Option - Isapi_Rewrite
I think you might be referring to "Pretty URLS" which is generally setup on a web server level using something like Apache mod_rewrite:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Categories