Welcome Guest, Not a member yet? Register   Sign In
Save php code in database
#1

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!
Reply
#2

(This post was last modified: 11-22-2020, 09:22 PM by ojmichael.)

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

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>
Reply
#4

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).
Reply
#5

PHP Code:
<head>
  <base href="<?= base_url(); ?>">
</
head
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB