CodeIgniter base_url() not working when called by cron job - php

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

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.

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/" : '';

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.

Related

Update links in Laravel for server from local

I statred creating project in Laravel. One of his method adds images to storage/app/public. To make this operation I had to call method:
php artisan storage:link
On my local server (I use to Xamp) it works perfectly. I have a problem after updating files on the external server. When I try open link to files in storage/app/public browser returns "error 404".
I wanted to call storage::link method on external server, but it didn't work (server blocks symlink() method).
I want to ask you do you know how resolve this problem? I have idea calling this method on local server, but with server url in parameters, but I don't know how do it.
Route::get('/storage-link', function (){
\Illuminate\Support\Facades\Artisan::call('storage:link');
});
Add this in web.php and hit the URL once and then remove it

How do I make the URL of my PHP application consistent on other machines?

I have a PHP application and I gave it to someone to test. Now, that person is writing http://10.0.2.111/Project/Webpage1.phpand it gets him to the correct page.
Now, when clicks on a link that links to other pages, the URL changes to http://localhostProject/Webpage2.php and it doesn't work.
How can I make to URL consistent?
PS: 10.0.2.111 is my machine's IP address.
So what's happening is your other pc is connecting via an IP address which is great. The code being sent back is using absolute URLs which is obviously broken on the client machine.
What you need to do is either..
Dynamic URLs
Make use of $_SERVER['HTTP_HOST'] for example
http://<?= $_SERVER['HTTP_HOST']; ?>/Annapoorna/Welcome_Page.php
This allows your code to run under different server names and addresses and will change accordingly
or
Relative URLs
Instead of specifying the hostname in the URL you can simply use a relative or root relative URL.. for example
/Annapoorna/Welcome_Page.php
This has the same affect as the above method but is handled client side, whereas the first method is worked out by the server

PHP links working in local host but breaking when hosting application on web server

I have web application. i am using WAMP server.
I have developed web application and put my application files under "login" and then in "www" folder of wamp.i.e. Drive:\wamp\www\login\
now when upload my file to the web host that goes directly to "www" folder and there is no "login" folder there. so my existing links on localhost e.g.
http://localhost:8081/login/dashboard.php
turning to http://example.com/login/dashboard.php.
now as there is no login folder, because of wrong path, link is breaking.
there is one way, that i change every link everytime before uploading to webserver, which is time consuming and prone to errors.
is there any simple way i.e. dynamic way that my links work perfect both on localhost as well as web server.
so that the link on localhost on web server the link should be http://localhost:8081/login/dashboard.php direct automatically to http://example.com/dashboard.php instead of http://example.com/login/dashboard.php
i tried using $_SERVER["DOCUMENT_ROOT"] but it point local drive path and did not work well for me..
any help really appreciated.
Have you tried to work with relative paths?
$path = "/login/etc..."
This way you don't have to change the paths each time. I think php even has a function to turn relative paths to absolute ones.

Serve static files in symfony and apache

I have a project in symfony and apache, also, I have a web/images folder where I store some images.
The problem is that when I type the following url: mydomain:app_dev.php/images/myimage.png
I get back this error:
No route found for GET /images/myimage.png
Any other url works fine.
Why is this happening? why cant this just be like a plain apache website where you type in the image url and get it back?
Thanks
Links that use static content should not use app_dev.php or app.php. This will allow it to be retrieved as if it was a 'plain apache website' or what ever web server you are using.
This should work the way you want it to.
mydomain:images/myimage.png

How to make apache serve file:///

I'm kind of new to HTML, PHP and stuff.
I'm trying to test locally a web site before putting it online. I set up my Apache server with PHP and MySQL and made a virtual server point localhost to
"C:/path/to/docroot/"
and everything works great.
Now in my index.html I have a link pointing to the file "mail-form.php" in the same directory (the C:/path/to/docroot/). In this link I only specified
href="mail-form.php"
and no absolute path, because I don't know which would be the absolute path in the production server and I don't feel like changing them all after testing. (Here I'm open for suggestions, if this is bad).
Now the thing I don't understand is the following: when I type in the browser's URL
"file:///C:/path/to/docroot/index.html"
and then click on the link, the browser tries to open
"file:///C:/path/to/docroot/mail-form.php"
and this doesn't get interpreted by PHP, but returned as text.
If I instead type in
"localhost/index.html"
and then click on the link, the browser calls
"localhost/mail-form.php"
and it gets interpreted properly showing what it should.
I can obviously live with this, but I'm curious if there is a way to make Apache/PHP serve the "file:///..." thing just as well as the "localhost/..." thing? After all they are both the same file. Or is it a browser problem? Or am I thinking wrong?
You can't make Apache serve file:///. Using that scheme instructs the browser to fetch the file directly from the filesystem. If you want to use Apache then you have to use http:// (or another URL scheme that makes a network request that Apache supports).
No. The file:/// protocol is not HTTP. The browser won't even send it to the localhost server you're running, and instead just read the file.

Categories