In PHP how to detect the execution is from CLI mode or through browser

0 votes
I have a common script which Im including in my PHPcron files and the files which are accessing through the browser. Some part of the code, I need only for non cron files.

 How can I detect whether the execution is from CLI or through browser (I know it can be done by passing some arguments with the cron files but I dont have access to crontab). Is there any other way ?
Oct 22, 2020 in PHP by kartik
• 37,520 points
1,372 views

1 answer to this question.

0 votes

Hello @kartik,

Use the php_sapi_name() function.

if (php_sapi_name() == "cli") {
    // In cli-mode
} else {
    // Not in cli-mode
}

Hope it helps!!

Thank You!!

answered Oct 22, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How to increase the execution timeout in php?

Hello @kartik, You need to change some setting ...READ MORE

answered Sep 1, 2020 in PHP by Niroj
• 82,880 points
1,140 views
0 votes
1 answer

How to get the home directory from a PHP CLI script?

Hello @kartik, You can fetch the value of ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,880 points
3,924 views
0 votes
1 answer

How to get the http headers from current request in PHP?

Hello, Try this code: if (!function_exists('getallheaders')) { ...READ MORE

answered Nov 23, 2020 in PHP by Niroj
• 82,880 points
2,746 views
0 votes
0 answers

How to detect faulty JPEG in PHP? (Issue with SOFn, DQT or DHT JPEG marker missing before a JPEG SOS marker)

Is there a way to detect if ...READ MORE

Jun 7, 2022 in PHP by Kichu
• 19,050 points
838 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,870 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,928 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,863 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,194 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
3,308 views
0 votes
1 answer

How to connect to SQL Server database from JavaScript in the browser?

Hello @kartik, You shouldn´t use client javascript to ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,880 points
23,876 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP