GET method and PHP - php

I have two questions.
In my webpage (developed by me), I use the GET method to return for example the id of one record that I have in a mySQL database.
For example, one url can be this:
http://www.example.com/example.php?id=1
But in MediaWiki software, they can use this GET method, or one strange method that I don't know, that works like this:
http://www.example.com/example.php/1
How can I do that my PHP script knows that what the id is 1?
And now I have another question. In MediaWiki software, you can do that the index.php file converts to a virtual wiki directory.
Now the final question is: How can I do that in my webpage, the url for this request to my mySQL database (with a PHP script) is:
http://www.example.com/example/1
Thanks.
P.D: Sorry about my bad English. I'm still learning, because I'm from Spain and in Spain the Enslish is not an official language.

You can do that with rewrites (e.g. via .htaccess)
Have a look at this page: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Apart from using mod_rewrite, if you're using Apache+PHP, you can use the $_SERVER['PATH_INFO'] var to accomplish the same thing.

Use mod_rewrite for the URL - and write a php backend which will parse the url. (fetch the get values)
This link should get you started, http://www.generateit.net/mod-rewrite/

Related

Why long URL's and what are those extra characters for?

I was looking at a few web e-commerce website and noticed that they usually have long URL something like this (taken from an e-commerce site)
https://www.example.com/bsquare-tuxedo-solid-men-s-suit/p/itmejr4zbjt6fr3h?gclid=CjwKEAiAz4XFBRCW87vj6-28uFMSJAAHeGZbXBpy4Rw5UckmBAiPaacHLhr5MbPj4bMxVThaQe5A3xoCIi7w_wcB&pid=SUIEJR4ZAK7FZKRT&cmpid=content_suit_8965229628_gmc_pla&tgi=sem%2C1%2CG%2C11214002%2Cg%2Csearch%2C%2C95089233620%2C1o5%2C%2C%2Cc%2C%2C%2C%2C%2C%2C%2C&s_kwcid=AL!739!3!95089233620!!!g!182171694500!&ef_id=WJitiQAAAfz4lw1Y%3A20170213091758%3As
Generally a router would be
http://www.example.com/controller/method/param1/param2
Then what all codes go up in the longer URL as above and why do a longer version when a short URL can get the same job done?
You can start to read more about get method:
http://www.html-form-guide.com/php-form/php-form-get.html
To send data server side in php you will need to make a request get or post. The second link is an get but rewritten.In the end to access your data the url will be rewritten by the framework you use ( http://www.example.com/controller/method?id1=param1&id2=param2 ). If you do not use any framework you will need to rewrite yourself with .htaccess Usually the second link is used for
more common pages so will be better indexed by google. The first link is for data that is more dynamic and may have more or less parameters, null or "" values etc. And also you do not care to index that page. For exempla a search : http://www.example.com/controller/method?searchValue=&page=1
You also can read more here. It explain how and why we rewrite links: https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/

Getting value from database to a folder name PHP&MYSQL

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/

Friendly subfolder link to page (wikipedia style)

I am struggling to find this as I'm not sure exactly what I'm looking for!
I've seen that some sites seem to use 'subfolders' as links to pages, e.g. wikipedia.
E.g. Let's say I had a record in a database with a unique title "My Shopping List". I want to be able to navigate to www.mySite.com/my_shopping_list and have it automatically 'forward' to a page showing the relevant record.
I am using IIS and wondering if there's a way I can do it with that, but I may be looking in the wrong place.
This is for a knowledgebase where I might write an article called "how to use a computer" and would like to be able to create a 'friendly' link for users (http://www.mySite.com/how_to_use_a_computer rather than http://www.mySite.com/article.php?ID=123. I will need to be able to create these 'links' dynamically using the title field.
Thank you
EDIT: I am using IIS6
You should use URL Rewrite, the microsoft ressource for this technique is here: http://www.iis.net/download/urlrewrite
In the iss forum the issue about url rewrite in iss < 7 is discussed with some suggestions on software to use.
I don't know which one is best, maybe there are others who know:
http://iirf.codeplex.com/
http://www.isapirewrite.com/
http://urlrewriter.net/
With PHP, you'll need to code URLConf using something like a htaccess file or the manager GUI. You can read ISS docs on URL rewrite to learn how., but as I understand, this is a module you'll have to download separately.
This question also shows how do do it without editing server configuration files or htaccess, but you'll be stuck with a http://example.com/index.php/path/to/view format (where index.php is a real script with the URLConf in it.)
Well some time ago it was used to call 'Friendly URL' or 'FURL', now i see that it's ust Rewrite URL. Look for tutorials for beginners.

PHP - Getting information from link itself rather than GET parameters?

Consider a situation of a URL as:
www.example.com/softwares/download.php?f=firefox&v=13
It does not look as good as URL:
www.example.com/softwares/firefox/download?v=13
or same download.php used as:
www.example.com/softwares/chrome/download?v=20
How can I achieve this type of URL filtering in PHP?
Some point I want to covered here:
I don't need a folder-hierarchy here like having different folders for /firefox/ and different for /chrome/
There could be only two PHP files (as I wish to) for all products: /software/software-info.php and /software/download.php (already got here).
I am able to put and fetch information from database in PHP but just want to have different link for different product.
I am a Java Web Developer in which you have Filters to get information from a part of link and redirect the request accordingly.
I am new in PHP programming and if this question is already asked or obvious than please pardon me and provide that question link.
This is ideally what you should use url rewriting (mod-rewrite in .htaccess) for.
Your visitor navigates to:
www.example.com/softwares/firefox/download?v=13
or even
www.example.com/softwares/firefox/13/
but your server will understand it as:
www.example.com/softwares/download.php?f=firefox&v=13
You can use htaccess files to do URL rewriting. Essentially this would allow you to take the segments of the url after /software/ and pass them ass parameters to be controlled by the software script.
There are also a bunch of PHP frameworks which use 'routes'. They're based on a similar principal as URL rewriting. I'd recommend Codeigniter as a good starting point - it's a straightforward framework, which plenty of documentation and tutorials.
Good luck!

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

Categories