Getting value from database to a folder name PHP&MYSQL - php

I am having a problem with a PHP script, so the link is like this: page.php?id=1412.
But I don't want to get the data using $_GET['id']; is there a way for the program to recognize the URL like this localhost/1412, and then try to find the 1412 in the id field in database?
If possible how can I avoid the Not Found page ? since the folder 1412 does not exist ?

For all of this kind of matching you need to use either .htaccess files for apache, or similar functionality implemented in the configuration files of nginx/lighthttpd and so on. Basicly, when a request like x/1412 will come, you can match it before being dispatched to the php engine and transform it internally into x?id=1412 . Other solution involves redirecting all requests to a front controller (a script that is serving as a single entry point into the application) and let a url matcher match the route to execute. The latter is implemented in about every big/small php framework.

You could regex the current uri ($_SERVER['REQUEST_URI']). See for creating regex: PHP URL Regex and Parameter

.htaccess file is a solution, you can access this type of url and can send the value to php files using .htaccess file refer this nice article for doing this
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Related

PHP Filename Parameter

I have seen various sites now defining the products name in the URL ... for example
http://www.webesite.co.uk/hp/hp-c9701a-cyan-toner-cartridge-original
instead of
http://www.webesite.co.uk/product.php?product=hp-c9701a-cyan-toner-cartridge-original
I am aware and have used a lot the $_GET function to get parameters specified in the URL after the .php file tag, but my question is how do you provide a parameter to replace the file name rather than specifying it at the end of the file name?
I'm working in PHP but I'm not sure if this is achieved using the .htaccess file
I am also interested to know which will perform best SEO wise???
Yes you need to use the mod_rewrite with .htaccess to redirect to the correct resource.
Here is some usefull link that explaim better what you actually need to do in .htaccess:
http://zenverse.net/seo-friendly-urls-with-htaccess/
And here is a similar thread in Stackoverflow:
How to create friendly URL in php?
It's usually done by redirecting every request to index.php using .htaccess and than by parsing (explode()) URL you can get every part of it and treat as a parameter.
You can also read about routing and router classes which are designed to do this work for you.

PHP CMS web page short URL?

I am buildiig a simple CMS and would like to know how to create short URLs (not the APACHE bit but the PHP bit).
example.com/?page=100
example.com/home/test
How would I interpret the ?page=100 into /home/test (Through select the database, but i couldn't figure out how) I can see if just one level /home/test because you probably can have a zoneID, but when it comes to /home/test/test. I become lost
And how do I parse back the /home/test to the page id.
Plus is there anyone can show a bit idea for the database design as well?
These resources can be useful to you:
https://stackoverflow.com/a/120411/370290
http://www.symfony-project.org/book/1_0/09-Links-and-the-Routing-System
http://codeigniter.com/user_guide/general/urls.html
http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
You need some kind of mod_rewrite for your server side.
That will help you to send route data to index.php (or somewhere else) file without filename in adress string. Than some php file will analyze the route and give correct html.
ok i think you need to definitely need to look at the way you are going to do your routing (through mod_rewrite)..for example
1.you can rewrite the page www.example.com/test to ..www.example.com/index.php?page=test and implement a way of getting page by the page name..and returning an id if a page name exists ..if multiple entries exist then maybe the last modified will be given precedence over the otheers ..you can get the following book CMS Design Using PHP and jQuery helped me alot

php: newbie question about modrewrite

i've started using modrewrite under php - i wrote an .htaccess file and defined some rules.
but the main question is - how would i do the "opposite thing" of creating the modrewrite-links inside my web?
eg. i'm using this original url inside my website: /search.php?par1=abc&par2=def&par3=ghi
the correspondending modrewrite url would be /go/find/something/here/index.html
so i'd need to replace all html-links in my web using the ugly url with the mod-rewrite url.
what's the easiest way to do this? (function for creating urls, using a database ..)
thanks
As this should be a migration job, only done once, go for search/replace. Using a function or database would mean you have to do the migration for each request to your app. Which seems to be unnecessary.
A function. Simply because that way you can replace what the function does with minimal changes to other parts of the website. This is, for example, the way Drupal does it.

Php REST webservice: are those url are logical or physical?

Hi I'm trying to build the server side of an application using REST archtecture with PHP. later I will make a client for android to consume those services.
I already know what is a RESTful application.
but still got something confuse:
if I want to get retrieve information of a user, the url I should access is www.domain.com/user/123 according to REST. but that means for every user in DB I should make a .php?? that sounds illogical. or there are something like java which use logical url, or just rewrite the url to something like www.domain.com/getusr.php?id=123 ??
fixed: is url rewrite the correct way to interpret request url from clients for a webservice in php?
for example: if someone request www.domain.com/user/123, I should rewrite to www.domain.com/getusr.php?id=123
or maybe php is not for webservice?
thanks
You need to rewrite the url to www.domain.com/getusr.php?id=123 , that way you need only 1 php file , all requests in form of www.domain.com/user/123 would go to a single getusr.php file . You can achive this using apache mod_rewrite ( there are tons of examples around so i whont post another one ) .
Edit
Check this link , that contains a few good examples .
Or you can use this code bellow ( place it inside a ".htaccess" file at the root of you're web directory , where getusr.php should be placed allso )
RewriteEngine on
RewriteRule ^user/([0-9]+)$ getusr.php?id=$1
I like the way Drupal does it. Drupal uses a q parameter for all of its routes:
http://myserver/drupal/?q=node/234
Which, if mod_rewrite is enabled, becomes
http://myserver/drupal/node/234
This way, you just have to parse the contents of $_GET['q'].
It's cleaner than having a bunch of separate rewrite rules for every one of your routes.

Dynamic mod_rewrite or how to plan a dynamic website

I'm trying to make a clean url for a blog on a dynamic website, but I think that the problem is that I don't know how to plan the website schema.
I read about how to use mod_rewrite and all I found is how to make "http://www.website.com/?category&date&post-title" to "http://www.website.com/category/date/post-title". that's works o.k for me.
The problem is that If my url looks like "http://www.website.com/blog/?id=34" this method won't work as far as I got it.
So, I have two questions:
1. Is there a way to use mod_rewrite (maybe read from a txt file) to read the post title of my blog and rewrite my url by date and post-title?
2. Should I rewrite my website to query the data from one index file in the homepage and use mod_rewrite to write the nice url? should I query also the date and the title of the post instead just the post ID?
mod_rewrite used to rewrite requests and it has nothing to do with urls. You have to change urls by hands.
yes, it's most common practice, to query the data from one index file
no, you can't use mod_rewrite to write the nice url
yes, an id must be present in the url along with title. your engine will just throw title away and use only id to retrieve an article.
Take a look at SO urls for an example
What you're talking about is commonly referred to as routing and lots of examples exist of different ways to do it with PHP. The most common approach uses the frontcontroller pattern, which means in the simple case rewriting all URLs to a single php file and then having that file determine what content to show dynamically based on the URL.
The most popular PHP frameworks (CakePHP, Symphony, Codeigniter, etc.) all have routing code in them which you might be able to use or might serve as inspiration. Alternatively this article covers lots of the basics if you want to do it yourself: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
RewriteMap allows you to do all sorts of dynamic rewriting (text file, script, etc).

Categories