Should links be database driven or hardcoded in the php file?
As the title says: Should links be database driven or hardcoded in the php file? i am referring to all system links we see on websites, like the header, footer, privacy, navigational links, page links, etc. I am thinking a table like:
link_id, url, description.
One reason i开发者_开发百科 want to do it this way is for link tracking as i am not sure how else to track links if they are not db driven?
Link management and link tracking are two separate issues.
If you're interested in tracking how often links are clicked on, you are going to need to set up some kind of redirect scheme or javascript to record users clicking on them.
If you're just worried about link URLs changing down the road, it's probably best to just be thoughtful about how your PHP files are set up. For content that's included in many pages (like the header or footer), set up separate files for them and include them into the pages.
As much as possible you don't want to be copy and pasting content between PHP files. If you use includes thoughtfully there will only be a few files you need to update if those static links change.
Also, it's probably not a good idea to put link URLs that will be shown on every page in the database, as 1. your pages won't display if the DB isn't up and 2. you'll potentially be creating tons of extra database requests. At the least, limit using the DB to track people clicking on links to leave your site.
(If you're just trying to track how often people click on links between pages on your site, you're better off just using a web log analyzer on your http logs after the fact, or installing Google Analytics.)
I'm not sure I understand 100% correctly, but generally you want to minimize your interaction with the database as much as you can since it slows down your website considerably. If you want to track all the clicks on your links, for one it's not actually necessary to store the link itself in the database, because among other things that will make changing stuff on your site a lot more of a pain. And for two you'd probably be happier using an analytics application like Google Analytics or Mixpanel.
So for static links, you almost definitely want to hardcode them. Your web app might have some dynamic links, for instance if a user logs in and you want to customize some of the links that appear based on that user. For that it makes more sense to have something like a users table in the database with a user id, and have logic in your application that bases the links that are created off of that user id.
I wouldn't bother storing them in the database, but you can create a php file with an associative array of string->urls. That way you can refer to them by name. You can also make a function that takes the name of a url and some query string parameters and returns the url you wanted. a nice way of avoiding hardcoded urls, works for me
精彩评论