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,490 points
84 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,580 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,580 points
82 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,580 points
86 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,580 points
82 views
0 votes
1 answer

How to check if php session is already started or not?

Hello kartik, Use session_id(), it returns an empty string ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,580 points
1,296 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,580 points
5,642 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,580 points
653 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,580 points
498 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,580 points
6,222 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,580 points
284 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,580 points
6,274 views