CodeIgniter Forums
Save php code in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Save php code in database (/showthread.php?tid=78034)



Save php code in database - guerreroretirado - 11-22-2020

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!


RE: Save php code in database - ojmichael - 11-22-2020

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()?>"



RE: Save php code in database - sammyskills - 11-23-2020

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>



RE: Save php code in database - egranty - 11-23-2020

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).


RE: Save php code in database - InsiteFX - 11-24-2020

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



RE: Save php code in database - guerreroretirado - 11-25-2020

Thank you guys, very useful your insights.
Now I have my links working properly Smile