简体   繁体   中英

CodeIgniter base_url() not working when called by cron job

I created a PHP script that will be called once a day by the cron job. The cron job works fine and creates a HTML Email template. I have a development(localhost) environment and a test environment.

My only issue is inside the HTML Email template, there is an image that isn't rendering on my test environment. If I inspect the element, the image path is set to localhost/images/image_name.jpg. I don't understand why in test environment the path is set to localhost; it should be testdomain/images/image_name.jpg instead of localhost/images/image_name.jpg.

If I manually call the script through the browser in the url, I get the correct image path, and the image is fine in test environment. Why does it work when calling it manually and not when it is called by the cron job?

Yes, I'm autoloading the url helper in the autoload.php file.

Here is my image <img src="'. base_url() .'images/image_name.jpg" alt="" />

I'm tempting to load the url helper inside the PHP file and wait tomorrow to see if that works.

Any ideas what is causing this problem?

Thanks

Basically, if your $config["base_url"] isn't set, CodeIgniter tries to figure it itself from $_SERVER['HTTP_HOST'] .

So, when you access to the testdomain , the content of $_SERVER['HTTP_HOST'] is testdomain .

But if you let server access it from http://localhost/ it is "localhost"

This is probably why it does not work. You should either set a base url in $config["base_url"] , or (not recommended) hardcode the domain into the <image> tag.

Edit: You can also try giving it a dynamic-ish base url, check the below code:

$config["base_url"] = !isset($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST'] == "localhost" ? "http(s)://testdomain/" : '';

Base URL should be absolute, including the protocol:

$config['base_url'] = "http://somesite.com/somedir/";

If you are leaving it empty CodeIgniter will try to autodetect the url.

I figured it out. The image is hosted on a private server. When I call the script through the browser (for testing purpose), the image is fine because I'm logged in as a user. The story is different when it is called by a cron job. When the cron job calls the script, the web app has no idea who you are. The purpose of the cron job, it to send out an email after 3 days registering, and when a customer receives the email, the image is broken because it is hosted on a private server. Since we are working on 3 different web apps and one of them is a public server, I stored the image on that public server and added a constant to point to the new location on the public server. Even though we are working on 3 different web apps, all apps are somewhat connected to each other, and it made sense to store the image on a public server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM