Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PHP CLI – Restrict to CLI Only

Posted on October 27, 2015 By David Kittell

Sometimes it’s easier to run a PHP script than to run a UNIX bash/shell script, this code below will require the use of PHP CLI and prevent it from running in a web browser.

Typically a good idea to place the PHP file out of the web directory as well.

This code will not really do much but you can get the idea of how it works.

<?php
 
// Restrict access to this file - start
if (php_sapi_name() != 'cli'){
    exit;
}
// Restrict access to this file - stop
 
// Require CLI Parameter - Start
// Require a username to get passed via CLI, if no username is received we will display an error at the end
if (isset($argv[1]) && ('' != trim($argv[1])))
{
    $username = $argv[1];
    echo $username;
}
else
{
    echo "No Parameter sent, exiting now";
    exit;
}
?>
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PHP

Post navigation

Previous post
Next post

Related Posts

Mac OS X – Remove Users In Terminal/SSH

Posted on February 19, 2016March 30, 2016

Recently I had to remove users from a Mac that was on a network only with SSH available. In this document I use the user name “administrator” as the account that I do the work in and would like to keep, your user name may be different. First we need…

Read More

PowerShell – Azure – Get-AzurePublishSettingsFile

Posted on September 13, 2016

Get-AzurePublishSettingsFile mkdir c:\temp mkdir c:\temp\PowerShell mv ‘~/Downloads/*.publishsettings’ c:\temp\PowerShell Import-AzurePublishSettingsFile -PublishSettingsFile ‘C:\temp\PowerShell\*.publishsettings’ All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not…

Read More

JSON From Web

Posted on June 18, 2015October 26, 2015

Use NuGet, search and install “Json.net” from Newtonsoft Visit json2csharp.com with the sample Json formatted text. json2csharp will create the public class for your json file/text Paste your Json text into the text box and click on the “Generate” button Copy the generated class into your code Create a dataGridView…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes