Jump to content

PHP if variable


gavin.sibley

Recommended Posts

Hello,

 

i am new to php so sorry if this is a dumb and basic question! i need to write a script so that if the clients ip address is "allowed" it will open a media player, and if there ip address is not on the list it will display an error.

 

i have the code for the various different bits, just not sure how to put it all together!

 


if $_SERVER['REMOTE_ADDR']; is &Allowedip

&Allowedip = "127.0.0.1"

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<!-- The one required parameter is the location of your audio file, which is defined using the "filename" parameter: --> 

<param name="filename" value="">

<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer" width=320 height=240></embed>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<param name="filename" value="">
<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer"></embed>

</object>

else
  
echo "Access denied from outsite of School"

?/>

 

Link to comment
Share on other sites

I think you need to look at some of the basic PHP syntax with some beginner examples.

 

PHP open and close tags:

<?php
  //PHP code here
?>

 

Variables are defined with a $ not a &:

<?php
  $variable = "Something";
?>

 

If syntax

 

<?php
  if(statement){
    //do something here
  }
} 
?>

 

Also, you need to tell PHP that you have finished a statement with a semi colon.

 

Infact looking at your code more, and more importantly the order of your code, there is a lot of work. You really should type beginners PHP tutorials into Google and go through a few basic lessons. I don't mean to sound condescending by the way, it's hard to type how I sound lol.

 

Edit: thought I would help anyway:

 

<?php
  $user_ip = $_REQUEST['REMOTE_ADDR'];

  if($user_ip == "111.111.111.111"){
    //echo out the html or exit php here
  }
?>

Link to comment
Share on other sites

ok i think im getting there slowly..... haha

 

what i have now is this

 


<?php  

  $AllowedIP = "127.0.0.1";

if($ip=@$REMOTE_ADDR;){    
    
//do something here

<html>
<body>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<!-- The one required parameter is the location of your audio file, which is defined using the "filename" parameter: --> 

<param name="filename" value="">

<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src=" " name="MediaPlayer" width=320 height=240></embed>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<param name="filename" value="">
<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer"></embed>

</object>

<body>
<html>


  }

else
{
     echo "Access Denied outside of School";

} 



?>

 

the only bit i dont know is how to make the statement. i.e if client ip equals $AllowedIP

 

thanks for your help,

gavin

Link to comment
Share on other sites

More like this

 

<?php
  $user_ip = $_REQUEST['REMOTE_ADDR'];
  $allowed_ip = "xxx.xxx.xxx.xxx"

  if($user_ip == $allowed_id){ //no semi colon here
    ?>
    Your HTML here
    <?php
  }else{
    echo "Access denied";
  }
?>

 

You can't just put HTML inbetween the php tags, you will need to exit php with ?> and then put your html in before starting php again with <?php

 

Or you can just echo out your HTML, there are other ways to but it would be best to get this concept down first

Link to comment
Share on other sites

ok thanks once again for your help. i think i have done it as you said...

 

<?php  

  $user_ip = $_REQUEST['REMOTE_ADDR'];  
  $allowed_ip = "127.0.0.1"  

  if($user_ip == $allowed_ip){ //no semi colon here    
     ?>    

<html>
<body>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player 

components..." type="application/x-oleobject" 

codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<!-- The one required parameter is the location of your audio file, which is defined using the "filename" parameter: --> 

<param name="filename" value="">

<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer" width=320 height=240></embed>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player 

components..." type="application/x-oleobject" 

codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<param name="filename" value="">
<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" 

name="MediaPlayer"></embed>

</object>

<body>
<html>    

<?php  

}

else
{
     echo "Access Denied outside of School";

} 

?>

 

however i am able to access this from any IP address, it doesnt seem to be stopping any addresses that arent registered.

 

thanks,

gavin

Link to comment
Share on other sites

Hello Zephni,

 

i am aware that 127.0.0.1 is the localhost IP address and when running this on my server i am changing that to the address of the computer that i want to be able to access the page. Just didnt want to post it on here with a clients IP address waving at everyone :)

 

Muddy_Funster, what i mean is that as the code stands, i am able to access this page from any web connected PC, even if that machine has a different external IP address. What i need it too do, is only show the page from my clients network (static IP address), and to everyone else just show an error message saying access denied. PHP on server is 5.3.10, is a cent OS linux box, and ive tried .php and .html.

 

.php comes up saying page doesnt exist

 

.html works and the content loads and works perfectly, apart from its showing everyone the content not just the assigned ap address.

Link to comment
Share on other sites

.... Unless you have told your page otherwise using a .htaccess or maybe another method it won't be able to proccess PHP with a .html page. I don't see why it is saying the page does not exist unless you are leaving "page.html" in the URL bar and forgot to change it to "page.php". Are you testing this on a webserver or are you running PHP on the machine?

Link to comment
Share on other sites

.php comes up saying page doesnt exist

 

.html works and the content loads and works perfectly, apart from its showing everyone the content not just the assigned ap address.

if you use the .html extension, unless you have made some very specific configuration settings in your PHP and apache config's, the PHP compiler will not even look at the code on the page, thus nothing in the php will be processed, thus the html is displayed regardless.

 

By default you must use .php extensions for your .php files so that the PHP precessor knows to action them.

Link to comment
Share on other sites

yes the page.php is definately on the server. When loading the page it comes up saying

 

 

 

The website cannot display the page

 

HTTP 500

 

Most likely causes:

•The website is under maintenance.

•The website has a programming error.

 

 

however, from what i can see the code looks to be all in order.

 

<?php  

  $user_ip = $_REQUEST['REMOTE_ADDR'];  
  $allowed_ip = "xx.xxx.xxx.xx"  

  if($user_ip == $allowed_ip){ //no semi colon here    
     ?>    

<html>
<body>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<!-- The one required parameter is the location of your audio file, which is defined using the "filename" parameter: --> 

<param name="filename" value="">

<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer" width=320 height=240></embed>

<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"> 

<param name="filename" value="">
<param name="Showcontrols" value="True">
<param name="autoStart" value="True">

<embed type="application/x-mplayer2" src="" name="MediaPlayer"></embed>

</object>

<body>
<html>
   
<?php  

}

else
{
     echo "Access Denied outside of School";

} 

?>

 

 

Link to comment
Share on other sites

<?php
    function visitor_ip(){ 
      if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
          $TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
      else $TheIp=$_SERVER['REMOTE_ADDR'];

      return trim($TheIp);
    }

    $user_ip = visitor_ip();
?>

 

Do that before the if statement happens, if you still struggle I can sort out all the code for you. But you should give it a shot first

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.