Save php code in database |
Hi,
I am a newbie in codeigniter and web development in general. I am developing a blog, and I am saving the posts ocntant in the database. The problem comes when the post content has links with php variables inside it: Code: <a href='<?=base_url()?>/blog/post-lug'>text in link</a> I wonder which is the best practice to save posts with this kind of links inside it. One optoin would be to go one by one and hard-code them. But, what if i still don't know the base_url? (maybe a very stupid and basic question, but I can't find a solution) Thank you in advance!
Well, I'll advise that do this instead:
1. Use relative links structure for links that are within the main url, which in your case is the `base_url()` function. So, your link within the post content should be something like so: Code: <a href="/blog/post-slug">text in link</a> 2. Use absolute link structure only when the link is redirecting to an external website, like so Code: <a href="https://google.com">text in link</a>
Saving PHP code in database commonly is a bad practice:
- after get a record from DB you have to parse all PHP codes and use eval() to execute those. It's a direct way to get XSS/SQL-injection. - you end up with non transparent code that is difficult to debug when looking for errors. Use relative URLs as was advised above. Never use hardcoded absolute URs with domain name specified. Otherwice you'll get a headache in case of use preffix like www. or subdomains m. for mobile version (and with http:/https too). PHP Code: <head> What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thank you guys, very useful your insights.
Now I have my links working properly ![]() |
Welcome Guest, Not a member yet? Register Sign In |