Jump to content

Ban / Unban / Unban All / Who's banned ~ ARRAY HELP LOL


GrantDouglas

Recommended Posts

As you will realise by reading the script i've attempted to write you will understand im fairly new to php and thus i need major help :)

You can see what im trying to do :) 

The ban works fine but its not in an ideal format. If someone could maybe fix up what iv written so i can do the following:

 

Ban user/Unban user/Unban all/ Check who's currently banned.

 

I know it's alot to ask but it's part of a large project a friends doing and i thought i was capable of it.

Thanks in advance guys, you've been a great help so far.

<?php
$banipfile = 'logs/banned.php';
$deny = file($banipfile);
$choicevalue = $_POST['choice'];
$user = $_POST['user'];


if ($choicevalue == "ban")
{

include("logs/banned.php");
if(in_array($user, $deny))
{

echo "The user you are trying to ban is already banned.";
}
else 
{
$banned = fopen($banipfile, "a"); 

fwrite($banned, "<?php\n");
fwrite($banned, "\$ip = array();\n");
$string = "\$ip[] = '"."".$user."'\n";
fwrite($banned, $string);
fwrite($banned, "?>\n");

fclose($banned); 
echo "The user: $user has now been banned";
}
exit();
}

if ($choicevalue == "whosbanned") 
{
include("logs/banned.php");
   $keys = array_keys($banipfile);
echo "The following ip's are currently banned:";
echo "<br>";
           print_r($keys);
exit();
}

if ($choicevalue == "unban") 
{
if(in_array($user, $ip))
{
unset($user,$ip);
echo "The user: $user has now been unbanned.";
}
else 
{
echo "The user is already unbanned.";
}
exit();
}
if ($choicevalue ==" unbanall") 
{
unset($ip);
echo "All users have no been unbanned.";
exit();
}

?>

Link to comment
Share on other sites

logs/banned.php code please

 

also use cases

 

also shouldn't the unban all blank the bans file?

 

also do bans based on month like write to a new file year.month.bans.php

then if someone unbans all by accident you can quickly ban again

 

o wait

 

  include("logs/banned.php");

  if(in_array($user, $deny))

 

  include("logs/banned.php"); <<<< isn't that useless?

Link to comment
Share on other sites

its just empty. Its an emtpy file so that when i ban someone they get inserted into the logs/banned.php document. and in the main script it has

include("logs/banned.php");
if(in_array($_SERVER['REMOTE_ADDR'], $ip)){
echo "You are banned from using this service for inappropriate usage.";
exit;
}
else {
echo "Welcome";
}

Link to comment
Share on other sites

And yes the unbanall should blank the file thats the plan :)

 

the banned.php file is initially an empty document.

 

i just need someone to help fix it so it does the things i mentioned..

 

ban/unban/unban all/whos banned.

 

If you can make it ban by month then that would be super but all i need really is to get those 4 functions working :D

 

And yes alot of it is useless as i added stuff by trial and error until things worked. Thats why theres unused variables and stuff sorry.

 

Link to comment
Share on other sites

bans.php

 <?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>

 

I banned myself several times... and that's the output...

 

now, it's obvs it's not working !!!

so let's see what's going wrong!

Link to comment
Share on other sites

bans.php

 <?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>
<?php
$ip = array();
$ip[] = 'fuck'
?>

 

I banned myself several times... and that's the output...

 

now, it's obvs it's not working !!!

so let's see what's going wrong!

 

 

I knew the above would be the output i just didnt know how to make it write to in the form of

<?php
$ip = array();
$ip[1] = 'iphere'
$ip[2] = 'iphere'
$ip[3] = 'iphere'
etc..
?>

 

 

Link to comment
Share on other sites

Here is what i want but written in english :) ..

if user chooses ban 
{
check if banned already; if they are banned then exit
   else 
   {
open logs/banned.php
and add the ip to an array in the example format:
"<?php
$ip = array();
$ip[1] = 'iphere'
$ip[2] = 'iphere'
$ip[3] = 'iphere'
etc..
?>"
}

if choice is whos banned
{
  open logs/banned.php
  print back the ips that are currently banned.
}

if choice is unban
{
check if they are banned; if so then unban them
   else
   {
   print the user is not currently banned.
   }
}
if choice is unbanall
{
open logs/banned.php
clear the array so it's ready to start fresh
}

Link to comment
Share on other sites

gonna be messy... but if you want it like that.. ok.. just hold on

 

I got it to just give

 

ip1

ip2

ip3

 

in the banlist

 

<?php
$banipfile = 'banned.php';
$deny = file($banipfile);

$choicevalue = $_GET['choice'];
$user = $_GET['user'];


if ($choicevalue == "ban") {
   if(!in_array("$user\n", $deny)) {
   $banned = fopen($banipfile, "a"); 

   fwrite($banned, "$user\n");

   fclose($banned);
   echo "The user: $user has now been banned";
   } else {
      echo "The user you are trying to ban is already banned.";
   }
exit();
}

if ($choicevalue == "whosbanned")
{
   $keys = array_keys($deny);
   echo "The following ip's are currently banned:";
   echo "<br>";
   foreach ($deny as $number => $banned) {
	$number += 1;
	echo "$number Banned: $banned<br />";
   }
   exit();
}

if ($choicevalue == "unban")
{
   if(SOMETHING)  //NEED TO WORK ON TIHS
   {
   echo "The user: $user has now been unbanned.";
   }
   else
   {
   echo "The user is already unbanned.";
   }
exit();
}
if ($choicevalue ==" unbanall")
{
// NEED TO WORK ON THIS
   echo "All users have no been unbanned.";
exit();
   }

?>

Link to comment
Share on other sites

<?php
$banipfile = 'banned.php';
$deny = file($banipfile);

$choicevalue = $_GET['choice'];
$user = $_GET['user'];


if ($choicevalue == "ban") {
   if(!in_array("$user\n", $deny)) {
   $banned = fopen($banipfile, "a"); 

   fwrite($banned, "$user\n");

   fclose($banned);
   echo "The user: $user has now been banned";
   } else {
      echo "The user you are trying to ban is already banned.";
   }
exit();
}

if ($choicevalue == "whosbanned")
{
   $keys = array_keys($deny);
   echo "The following ip's are currently banned:";
   echo "<br>";
   foreach ($deny as $number => $banned) {
	$number += 1;
	echo "$number Banned: $banned<br />";
   }
   exit();
}

if ($choicevalue == "unban") {
$ban = array_search("$user\n",$deny );
if (isset($ban) && $ban != '') {
	unset($deny[$ban]);
	$banned = fopen($banipfile, "w+"); 
	foreach($deny as $line) { fwrite($banned,$line); }
	fclose($banned);
	echo "$user WAS UNBANNED!";
} else {   
	echo "THIS BRO WAS NOT BANNED IN THE FIRSY PLACE BRO!";
}
exit();
}

if ($choicevalue ==" unbanall") {
$banned = fopen($banipfile, "w+"); 
fwrite($banned,"");
fclose($banned);
echo "All users have no been unbanned.";
exit();
}

?>

 

so instead of including the ban file, use:

 

<?php $banipfile = 'banned.php';
$ip = file($banipfile);
if(in_array($_SERVER['REMOTE_ADDR'], $ip)){
echo "You are banned from using this service for inappropriate usage.";
exit;
}
else {
echo "Welcome";
}

 

 

The code I wrote puts it into

 

ip1

ip2

ip3

 

etc...

 

which is much easier to add/remove imo

 

then if you want to manually search + remove you don't have to reset keys on the array to make it neat :P

 

 

also unbanall has a space before it - I'm unsure if this was intentional so left it in there.

 

 

you might want to add some && $user != '' or something just so it doesn't break if no user is specified

 

 

//edit

oops - if (isset($ban) && $ban != '') {

Link to comment
Share on other sites

no, 1sec

oops

 

<?php
$banipfile = 'banned.php';
$deny = file($banipfile);

$choicevalue = $_GET['choice'];
$user = $_GET['user'];


if ($choicevalue == "ban") {
   if(!in_array("$user\n", $deny)) {
   $banned = fopen($banipfile, "a"); 

   fwrite($banned, "$user\n");

   fclose($banned);
   echo "The user: $user has now been banned";
   } else {
      echo "The user you are trying to ban is already banned.";
   }
exit();
}

if ($choicevalue == "whosbanned")
{
   $keys = array_keys($deny);
   echo "The following ip's are currently banned:";
   echo "<br>";
   foreach ($deny as $number => $banned) {
	$number += 1;
	echo "$number Banned: $banned<br />";
   }
   exit();
}

if ($choicevalue == "unban") {
$ban = array_search("$user\n",$deny );
if (isset($ban) && $ban != '') {
	unset($deny[$ban]);
	$banned = fopen($banipfile, "w+"); 
	foreach($deny as $line) { fwrite($banned,"$line\n"); }
	fclose($banned);
	echo "$user WAS UNBANNED!";
} else {   
	echo "THIS BRO WAS NOT BANNED IN THE FIRSY PLACE BRO!";
}
exit();
}

if ($choicevalue =="unbanall") {
$banned = fopen($banipfile, "w+"); 
fwrite($banned,"");
fclose($banned);
echo "All users have no been unbanned.";
exit();
}

?>

 

works fine here now :P

 

http://localhost/connect.php?choice=ban&user=ip1

The user: ip1 has now been banned

http://localhost/connect.php?choice=ban&user=ip2

The user: ip2 has now been banned

http://localhost/connect.php?choice=ban&user=ip3

The user: ip3 has now been banned

http://localhost/connect.php?choice=ban&user=ip4

The user: ip4 has now been banned

http://localhost/connect.php?choice=ban&user=ip1

The user you are trying to ban is already banned.

 

 

 

HMMMM

 

doesn't want to see the first ip in the list :(

 

Link to comment
Share on other sites

Never mind i got it, iv added it all into the correct postition, it all works now including unban all (and i fixed that space, thank you). just the only one last thing is unban user returns "the user is not currently banned" when they are banned and it wont unban them. any ideas?

Link to comment
Share on other sites

something here doesn't work

if ($choicevalue == "unban") {
   $ban = array_search("$user\n",$deny );
   if (isset($ban) && $ban != '') {
      unset($deny[$ban]);
      $banned = fopen($banipfile, "w+"); 
      foreach($deny as $line) { fwrite($banned,$line); }
      fclose($banned);
      echo "$user was unbanned";
   } else {   
      echo "The user is not currently banned";
   }
   exit();
}

Link to comment
Share on other sites

Omg sorry another aswell lol i feel so bad asking all this help :(

<?php
$banipfile = 'logs/banned.php';
$ip = file($banipfile);
if(in_array($_SERVER['REMOTE_ADDR'], $ip)){
echo "You are banned from using this service for inappropriate usage.";
exit;
}
else {
echo "Welcome";
}

 

When i ban myself this never stopped me using the script :(

Link to comment
Share on other sites

<?php
$banipfile = 'logs/banned.php';
$deny = file($banipfile);

$choicevalue = $_POST['choice'];
$user = $_POST['user'];


if ($choicevalue == "ban") {
   if(!in_array("$user\n", $deny)) {
   $banned = fopen($banipfile, "a"); 

   fwrite($banned, "$user\n");

   fclose($banned);
   echo "The user: $user has now been banned";
   } else {
      echo "The user you are trying to ban is already banned.";
   }
exit();
}

if ($choicevalue == "whosbanned")
{
   $keys = array_keys($deny);
   echo "The following ip's are currently banned:";
   echo "<br>";
   foreach ($deny as $number => $banned) {
	$number += 1;
	echo "$number Banned: $banned<br />";
   }
   exit();
}

if ($choicevalue == "unban") {
$ban = array_search("$user\n",$deny );
if (is_int($ban)) {
	unset($deny[$ban]);
	$banned = fopen($banipfile, "w+"); 
	foreach($deny as $line) { fwrite($banned,$line); }
	fclose($banned);
	echo "$user WAS UNBANNED!";
} else {   
	echo "THIS BRO WAS NOT BANNED IN THE FIRSY PLACE BRO!";
}
exit();
}

if ($choicevalue =="unbanall") {
$banned = fopen($banipfile, "w+"); 
fwrite($banned,"");
fclose($banned);
echo "All users have now been unbanned.";
exit();
}

?>

 

 

<?php
$banipfile = 'logs/banned.php';
$ip = file($banipfile);
if(in_array($_SERVER['REMOTE_ADDR'], $ip)){
echo "You are banned from using this service for inappropriate usage.";
exit;
}
else {
echo "Welcome";
}

 

works now :D

 

sorry about my buggy code >_<

 

 

 

btw, to check errors I used these

 

I used

echo var_dump($var);

to see what type was being returned (that was the problem - it returned a boolean false when it didnt find anything but the key of the array when it found something... in this case the keys are only integers so i used is_int :P )

 

and my trusty

 

echo nl2br(str_replace(' ',' ',print_r($array,true)));

 

to print a "browser friendly" array

Link to comment
Share on other sites

No it didnt work either. it only lets you unban the ip thats at the bottom of the list.

try it yourself. add a few ip's and it will unban the bottom one but the if you try the others it says that they arent currently banned . :(

 

<?php
$banipfile = 'logs/banned.php';
$ip = file($banipfile);
if(in_array($_SERVER['REMOTE_ADDR'], $ip)){
echo "You are banned from using this service for inappropriate usage.";
exit;
}
else {
echo "Welcome";
}

still allows me to use the script also :( sorry

Link to comment
Share on other sites

OOOOOOOOOOOOOPS!!!

each ip ends in a new line char (\n)

 

so instead of

if(in_array($_SERVER['REMOTE_ADDR'], $ip)){

use

if(in_array($_SERVER['REMOTE_ADDR']."\n", $ip)){

 

 

 

 

 

Each line is the url I used and the line below it is response

 

connect.php is the ban page

new.php is the are you aloud to view me page

 

MY ip is 127.0.0.1 (because i'm running my server localhost)

 

http://localhost/new.php

Welcome

 

http://localhost/connect.php?choice=ban&user=127.0.0.1

The user: 127.0.0.1 has now been banned

http://localhost/connect.php?choice=ban&user=127.0.0.2

The user: 127.0.0.2 has now been banned

http://localhost/connect.php?choice=ban&user=127.0.0.3

The user: 127.0.0.3 has now been banned

http://localhost/connect.php?choice=ban&user=127.0.0.4

The user: 127.0.0.4 has now been banned

http://localhost/connect.php?choice=ban&user=127.0.0.2

The user you are trying to ban is already banned.

 

 

http://localhost/connect.php?choice=whosbanned

The following ip's are currently banned:

1 Banned: 127.0.0.1

2 Banned: 127.0.0.2

3 Banned: 127.0.0.3

4 Banned: 127.0.0.4

 

http://localhost/new.php

You are banned from using this service for inappropriate usage.

 

 

http://localhost/connect.php?choice=unban&user=127.0.0.2

127.0.0.2 WAS UNBANNED!

 

http://localhost/connect.php?choice=whosbanned

The following ip's are currently banned:

1 Banned: 127.0.0.1

2 Banned: 127.0.0.3

3 Banned: 127.0.0.4

 

http://localhost/connect.php?choice=unban&user=127.0.0.2

THIS BRO WAS NOT BANNED IN THE FIRSY PLACE BRO!

 

http://localhost/connect.php?choice=unbanall

All users have no been unbanned.

 

http://localhost/connect.php?choice=whosbanned

The following ip's are currently banned:

 

http://localhost/new.php

Welcome

 

 

all works!

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.