Jump to content

PHP Newbie

duckwithanokhat

I've never heard of php until today and my dad wants me to write a php file as follows:

 

Create a variable that can execute the linux command "hostname"

Then show text and add that variable to it. 

 

This is how far I've gotten:

 

#!/bin/bash

$hostname=

 

Yea... please help!

Link to comment
Share on other sites

Link to post
Share on other sites

<?php 
	$hostname = exec('hostname');
	echo 'This machine\'s hostname is ' . $hostname;
?>

PHP has the ability to run commands from within a PHP file. Note that PHP files need to be run from a webserver. LAMPP is what you want for simplicity. Your PHP configuration has to be set so safe mode is off. Usually it is by default, but just in case. The exec function is not available when in safe mode. To run this script, make sure your PHP file is in the web root of your webserver and navigate to it from a web browser 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Hazy125 said:

<?php 
	$hostname = exec('hostname');
	echo 'This machine\'s hostname is ' . $hostname;
?>

PHP has the ability to run commands from within a PHP file. Note that PHP files need to be run from a webserver. LAMPP is what you want for simplicity. Your PHP configuration has to be set so safe mode is off. Usually it is by default, but just in case. The exec function is not available when in safe mode. To run this script, make sure your PHP file is in the web root of your webserver and navigate to it from a web browser 

Can you tell me what #!/bin/bash means? The examples that my dad showed me all started with #!/bin/bash so I just assumed that that would be how to start it.

 

Note: That works! And the php I'm using is for apache2 (if that makes sense),

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, duckwithanokhat said:

Can you tell me what #!/bin/bash means? The examples that my dad showed me all started with #!/bin/bash so I just assumed that that would be how to start it.

 

Note: That works! And the php I'm using is for apache2 (if that makes sense),

That means that he wants to run the script from the Linux command line rather than a browser. That's how you start a Linux script. You can run PHP scripts this way, but I've never done it. I believe you can do something like 

 

#!/bin/bash
/usr/bin/php /yourfile.php

To produce the same effect in the Linux command line, but as I said. Not too familiar with doing things this way

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Hazy's code is correct (Although I would add a newline character "\n")

 

You should be able to call it from the linux command line with

 

php yourFile.php

I just double checked on my vps and it worked

CPU: Intel Core i7-4770k | Mobo: MSI Mpower Max | Cooling: Cryorig R1 Ultimate w/ XT140 front Fan | GPU: EVGA GTX 770 Dual SC SLI | Case: NZXT H440 | Case Fans: Phanteks PH-140SP x5 | PSU: EVGA Supernova P2 1000W | RAM: 16GB Crucial Ballistix Tactical Tracer | SSD: Kingston HyperX 3k 120GB | HDD: Seagate Barracude

Keyboard: Razer Blackwidow Ultimate 2013 | Mouse: Razer Deathadder 2013 | Headphones: Sennheiser HD438s | Mousepad: Razer Goliathus Control | Monitor 1: Benq XL2430T | Monitor 2: BenQ RL2455HM 

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 30/04/2016 at 7:06 AM, duckwithanokhat said:

Can you tell me what #!/bin/bash means?

#!/bin/bash is used to define a bash script. This should never be at the start of a PHP script but you can use #!/usr/bin/php

 

By defining the language in the script the system will use the correct interpreter so for example without that line you would need to run:

 

    php myscript.php

 

But with it set you can just use:

 

    ./myscript.php

 

As mentioned above you can do this easily with PHP but creating another bash script to run the PHP script is pointless, just run PHP in the command line. You can read more about using PHP in the command line here http://php.net/manual/en/features.commandline.usage.php

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×