Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Shells Virtual Desktop
BMail.ag - Secure Email Service
Server.net
CPLicense.net
VPS Server
Buy VPN
Vultr
VMs for AI
HostDare
HostDare
ReliableSite White-Label Dedicated Hosting for Resellers
InterServer VPS
BMail.ag - Secure Email Service
Best VPN
High-Performance Bare Metal Server Solutions
Karvl.com
Server Mania Cloud Hosting
DataWagon Hosting
AlphaVPS Hosting
Evoxt.com
Clouvider
VPS Hosting with NVMe
Residential IPs in the US & 4G Mobile Proxies in EU & US with Unlimited Bandwidth
ReliableSite White-Label Dedicated Hosting for Resellers
Rabisu - Hosting Solutions
Shells Virtual Desktop
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Php script help

WHTWHT Member

Hello guys.

Can someone please help me to create a simple script to show results per IP?

Ex: if the request comes from IP 233.233.233.233 and this ip is added in the list should show this output: 11111

If the request comes from a IP that is not incluced in the list should show this output: 22222

Am searching in google but nothing can find.

P.s should be a PHP not .htaccess.

@Traffic maybe? :)

Thank you guys!

Comments

  • This ?

    Switch the echo to 111111, but that's a snippet of what I think you're looking for.

  • Something like this?

    <?php
    $listOfIPS = explode('\n', file_get_contents('ips.txt'));
    
    if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '11111';
    }
    
    else {
    echo '22222';
    }
    ?>
    
    Thanked by 2doghouch Jacob
  • @Kris said:
    This ?

    Switch the echo to 111111, but that's a snippet of what I think you're looking for.

    I don't think that's correct. The $_SERVER superglobal is an array, so it can't be compared to a string like that. @theroyalstudent's example looks good.

  • WHTWHT Member

    @theroyalstudent said:
    Something like this?

    > <?php
    > $listOfIPS = explode('\n', file_get_contents('ips.txt'));
    > 
    > if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    > echo '11111';
    > }
    > 
    > else {
    > echo '22222';
    > }
    > ?>
    > 

    This one is great but cant add more then 1 ip. Am trying with commas also in a new line but wont work :(. Also if is possible to show different result per ip will be perfect!!!!

  • edited January 2016

    @WHT said:
    This one is great but cant add more then 1 ip. Am trying with commas also in a new line but wont work :(. Also if is possible to show different result per ip will be perfect!!!!

    Maybe you need a database for that? Or you could work out something like an IP per line, in this format:

    <IP> <RESULT>

    I would just use a database though, if the dataset is gonna get bigger as time pasts.

  • agoldenbergagoldenberg Member, Host Rep

    Well he's exploding based on a new line so you should have something like

    1.1.1.1
    2.2.2.2
    3.3.3.3
    

    in your text file and it should work fine.

  • WHTWHT Member

    Cant create database due to lack of knownledge :)

    @agoldenberg said:
    Well he's exploding based on a new line so you should have something like

    > 1.1.1.1
    > 2.2.2.2
    > 3.3.3.3
    > 

    in your text file and it should work fine.

    Is not working. Only the first IP works but not others :(

  • @WHT said:
    Is not working. Only the first IP works but not others :(

    Here you go, use PHP_EOL instead;

    http://stackoverflow.com/a/29471912

    Thanked by 1WHT
  • WHTWHT Member

    Thank you but cant figure it out. Just to add that line in your script?

  • draziloxdrazilox Member
    edited January 2016

    Or \n in double quotes. Since '\n' is literally just string(2) "\n"

    Thanked by 1WHT
  • agoldenbergagoldenberg Member, Host Rep
    <?php
    $listOfIPS = explode(PHP_EOL, file_get_contents('ips.txt'));
    
    if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    echo '11111';
    }
    
    else {
    echo '22222';
    }
    ?>
    

    Here

    Thanked by 2theroyalstudent WHT
  • @agoldenberg said:

    > <?php
    > $listOfIPS = explode(PHP_EOL, file_get_contents('ips.txt'));
    > 
    > if (in_array($_SERVER['REMOTE_ADDR'], $listOfIPS)) {
    > echo '11111';
    > }
    > 
    > else {
    > echo '22222';
    > }
    > ?>
    > 

    Here

    Thanks - saved me from typing the code on my tablet, haha.

    Thanked by 1WHT
  • WHTWHT Member

    @drazilox said:
    Or \n in double quotes. Since '\n' is literally just string(2) "\n"

    Yeah the double quotes did the trick!!! Thanks.

    Thank you guys for the help! You are great!!!!

  • agoldenbergagoldenberg Member, Host Rep

    @theroyalstudent No worries mate! :)

    Thanked by 1theroyalstudent
Sign In or Register to comment.