Hello, confused in the operators ... Here is the code:

<?php echo "<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>"; $letters = "../letters.dat"; $data = @file($letters); $data = @array_reverse($data); $ncol = count($data); $ip = $_SERVER['REMOTE_ADDR']; $file = "ips.dat"; $ban = file($file); $count = count($ban); for ($i = 0; $i < $count; $i++) { if ($ip == $ban[$i]) { echo "Вы забанены"; }} else { if (file_exists($letters)) { for ($j = 0; $j < $ncol; $j++) { list($login, $photo, $message, $time) = split(":::", "$data[$j]"); echo "<table width='100%' class='message'> <tr><td><img src=$photo width='50' height='50'>&nbsp;$login</td><td width='50'>$time</td></tr> <tr><td colspan='2'>$message</td></tr></table>"; } } else { echo "<center><font color='silver'><h1>Записей нет</h1></font></center>"; } } ?> 

I need to make it so that when $ip == $ban[$i] it displays "You are banned", and if this is false , then such operators are output that go, starting with if (file_exists($letters)) ... Tell me please.

  • And what error does it display? - Oleg Arkhipov
  • Error: Parse error: syntax error, T_ELSE on line 14. 14 line - this is else {if (file_exists ($ letters)) ... Here you can like elseif ... well, or some operator where you can do so , then ban, if not, then output the following statements, where if there is a file, then output the data, and if not, output “No entries”. - ivanforpw 1:31

3 answers 3

Let me first suggest some important , in my opinion, points. Instead of $_SERVER['REMOTE_ADDR'] try using $_SERVER['HTTP_X_FORWARDED_FOR'] , sometimes it saves in the display of a real ip , not a proxy.

Also note that:

  for ($i=0; $i<$count; $i++){ if($ip == $ban[$i]){ echo "Вы забанены";}} else { 

It does not work else , because extra curly brace.

  • Error: Parse error: syntax error, unexpected T_ELSE ... Just because of the extra curly brace. PS Why do I have the code in the comments on the site does not highlight the syntax? - Maxim Tsybanov
  • about ip thanks, about the bracket ... I'm confused there, but it seems like the second from for ... - ivanforpw
  • Please write the code, as you see it correctly, I just did not understand a little. - ivanforpw
  • So what else should work if you if if you close a curly bracket from it? Your mistake is just because of the extra bracket :) - Maxim Tsybanov
  • Together ... echo "You are banned";}} else {... try ... echo "You are banned";} else {... - Maxim Tsybanov

IMHO this block makes sense

 $count = count($ban); for ($i=0; $i<$count; $i++){ if($ip == $ban[$i]){ echo "Вы забанены";}} 

replace with a simpler design

 if (in_array($ip, $ban)) echo "Вы забанены"; }... 

else goes further in the text

  • It displays No Records ... - ivanforpw
  • in which case when the user is banned? In your code, this phrase is displayed when the user's IP is not in the list of banned IPs and there should not be a file "../letters.dat", maybe there is no file yet? Why do not you specify the path to it relative to the DOCUMENT ROOT, and not relative to the current script? - zippp
  • I solved the problem differently, I decided not to float the brain on others and simplify the script. The script itself is just a piece that is included in the main script, I just executed this ban script in the main file, and I set the inclusion under the else. - ivanforpw

The bracket is not superfluous, just else stands NOT after the if , but after for .