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 in PHP by kartik
• 35,300 points
42 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 by Niroj
• 76,540 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 in PHP by Niroj
• 76,540 points
50 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 in PHP by Niroj
• 76,540 points
26 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 20 hours ago in PHP by Niroj
• 76,540 points
4 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 in PHP by Niroj
• 76,540 points
764 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 in Laravel by Niroj
• 76,540 points
3,441 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 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 in Laravel by Niroj
• 76,540 points
4,142 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 in PHP by Niroj
• 76,540 points
154 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 in PHP by Niroj
• 76,540 points
2,152 views