gavin.sibley Posted May 3, 2012 Share Posted May 3, 2012 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" ?/> Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/ Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342577 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342579 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342580 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342586 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 You may want to look at http://en.wikipedia.org/wiki/Localhost That is a localhost IP. You will want to find out your WAN IP here: http://www.whatismyip.com/ Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342587 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2012 Share Posted May 3, 2012 what do you meen "any" IP address? are you changing you IP address on that computer or using other computers to access the page? What file extension does this page have? what webserver are you running? what version of PHP are you using? Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342588 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342589 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 .... 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? Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342590 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2012 Share Posted May 3, 2012 .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. Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342591 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 testing on a webserver, definately changing the extension to .php but doesnt seem to exist! haha Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342593 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 In your FTP program have you refreshed your local machine window so the file changes to a page.php file before uploading? Is the page.php file actualy appearing on your webserver? Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342594 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342595 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 Semi colon at the end of statements Sorry as it looks like it was me who missed it in my example code... Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342596 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 Were getting closer! haha the autentication of the IP address seems to be working now, but in OVERTIME! haha it is now just blocking everyone from viewing the content, even those from within the specified IP address. Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342599 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 <!-- Accidental post --> Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342600 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 To test it, echo out the remote address like: <?php echo $_REQUEST['REMOTE_ADDR']; ?> If you dont get the expected result have a look at this: http://wiki.jumba.com.au/wiki/PHP_Get_user_IP_Address Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342601 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 This doesnt seem to be displaying anything... Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342612 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342617 Share on other sites More sharing options...
gavin.sibley Posted May 3, 2012 Author Share Posted May 3, 2012 This works perfectly, media player is working as it should and is authenticating the users on their IP address. brilliant!! thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342619 Share on other sites More sharing options...
Zephni Posted May 3, 2012 Share Posted May 3, 2012 No problem, happy coding Quote Link to comment https://forums.phpfreaks.com/topic/262002-php-if-variable/#findComment-1342620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.