Page 1 of 1

Need help writing a simple Php

Posted: Wed Jul 01, 2009 4:07 am
by thegeek6
Hi....I am a noob at PHP....and I need help with a PHP Script

I found this example on a website and i wanted to know if anybody could write this in PHP....and not need lnyx or bash to use it!!!!

Here is the script
#! /bin/bash

exip=`lynx -dump http://www.whatismyip.com | head -n 2`
echo "$exip"

Thanks in return.......

Re: Need help writing a simple Php

Posted: Wed Jul 01, 2009 5:25 am
by SeaJones
This can be hugely refined but:

Code: Select all

 
 <?php
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://www.whatismyip.com/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
    $result = curl_exec ($curl);
    curl_close ($curl);
?>
 
Before anyone slams me for the code, I grabbed it from a google search for "example Curl Php script".

That will return the entire page as $result, after which chopping it up into the bits you want is just a matter of string slicing and so on. If it's a particular value you want then consider using preg_match and regex to find it.

If you're simply after a script that will show your box IP:

Code: Select all

 
<?php
 
echo $_SERVER['SERVER_ADDR'];
 
?>
You could find this pretty quickly with google too, try searching "PHP what's server IP".

Re: Need help writing a simple Php

Posted: Wed Jul 01, 2009 7:55 am
by thegeek6
SeaJones wrote:This can be hugely refined but:

Code: Select all

 
 <?php
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://www.whatismyip.com/");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
    $result = curl_exec ($curl);
    curl_close ($curl);
?>
 
Before anyone slams me for the code, I grabbed it from a google search for "example Curl Php script".

That will return the entire page as $result, after which chopping it up into the bits you want is just a matter of string slicing and so on. If it's a particular value you want then consider using preg_match and regex to find it.

If you're simply after a script that will show your box IP:

Code: Select all

 
<?php
 
echo $_SERVER['SERVER_ADDR'];
 
?>
You could find this pretty quickly with google too, try searching "PHP what's server IP".
The Bottom one I've tried before.........I't returned 127.0.0.1 (definetly not right!!!)

The reason I need the script is i am running a webserver from home...and i have a dynamic IP and i am behind a router....so the script is supposed to return my outside ip......The Curl one worked....not in the way i wanted it to.....but it serves it's purpose.......

Thanks for the Help SeaJones!!!!!