how to generate links in php?
This is a very basic question but I couldn't seem to find anything on Google, hm.
Anyway, I'm not referrin开发者_开发技巧g to creating links, i'm talking of generating links like site.com/1 or how to generate links in php?, the numbers after the url are stored in the database with a corresponding post for example. How do I generate these using php?
If i visit h**p://site.com/questions/3870639/how-to-generate-links-in-php how do I tell the server to query the database and retrieve the corresponding post of 3870639? (without htaccess).
That's not possible with just PHP, it requires an Apache server module called mod_rewrite (assuming you use Apache for your server). Google for it.
You have to use .htaccess
.
You can implement these kind of URLs by forwarding all request to a specific file. This is your Front Controller and it will decompose the URL, extract the information and perform or delegate the necessary actions. With the front controller you have a single point of entry to your website.
But you need to set up .htaccess
to forward all requests to this file, e.g.
RewriteRule ^(.*)$ index.php [QSA,L]
You should check various frameworks like symfony or Zend, that implement this pattern.
I'm not sure how you would tell the server to navigate to a page if you went to a directory 3870639 that wasn't there. I would commanly use htaccess to redirect to page.php or similar and use $_SERVER['REQUEST_URI'] to get the page URL, or convert the URL to a $_GET variable in the htaccess you can then split('/',$_SERVER['REQUEST_URI']) to get an array of the directories.
$directories = split('/',$_SERVER['REQUEST_URI']);
print_r($directories);
gives
array(
[0] => 'site.com',
[1] => 'questions',
[2] => '3870639',
[3] => 'how-to-generate-links-in-php'
)
But like I said, no idea how to get to that page.
Text transforms. Write a regular expression that will spot a URL and transform it to <a href="$1">$1</a>
.
Using mod_rewrite would be the best solution, but if you can't use it you can still achieve something similar using MultiViews. You would, however, still need it enabled (normally done via .htaccess or the Apache conf).
MultiViews is meant for allowing content negotiation, so you can request /images/banner and Apache returns an appropriate type (banner.png, banner.svg, etc) depending on the browser's Accept header. You can use this to hide the extension of your scripts, allowing you to change the technology running your site without changing your urls.
In your case, you could use
/questions/3870639/how-to-generate-links-in-php
which would be the same as
/questions.php/3870639/how-to-generate-links-in-php
Within questions.php $_SERVER["PATH_INFO"]
would contain /3870639/how-to-generate-links-in-php
(or you could parse $_SERVER['REQUEST_URI']
as BenWells mentions). Use explode()
or a regular expression to get just the ID.
I don't think it is possible with PHP alone. You have to use mod_rewrite (which you do not wish to use) or change the default 404 error file to index.php. Then in index.php you can use explode('/', $_SERVER['REQUEST_URI'])
to obtain the id from the url.
But another way to do this is to use the url (I am not sure that this will work, but I think I have seen it somewhere) http://site.com/index.php/questions/3870639/how-to-generate-links-in-php
精彩评论