• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save php code in database


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: 

<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!

Save your posts in the DB without the base_url() then add a base tag to your template in the head section.

PHP Code:
<base href="<?=base_url()?>"

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:

<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

<a href="https://google.com">text in link</a>
Check out my blog: https://lagbaja.dev

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:
  <base href="<?= base_url(); ?>">
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 Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.