PHP - Quick Question

Soldato
Joined
11 Apr 2003
Posts
4,208
Location
Notts
Hi all, I have a guestbook, as you probably already know from my posts! But I was just wondering if there is a way to log IP addresses, of only the people that make posts, so:

If a user makes a post, then the normal post details + their IP address will be stored into the guestbook table, but they will not know their IP has been stored.

I want to do this because I have had a few problems of people breaking my page continualy, being abusive etc.

I would like then to be able to ban them... But if this is to complicated I am happy with just logging posters IP's... Thanks!
 
marc2003 said:
$_SERVER["REMOTE_ADDR"]

just assign that to a variable and add it to the database.

i guess to ban someone you would have to configure the webserver to do that?

edit: unless you want to let them access the site but not be able to post then i guess you could use a php/mysql query???
Thanks got it logging now :) Or at least it logged mine correctly!

What kind of a query would I have to use to ban the ip from posting?
 
robmiller said:
Code:
$banned_ips = array('123.456.7.8', '12.3.45.6');

if ( in_array($_SERVER['REMOTE_ADDR'], $banned_ips) )
    die("banned sry");
How would I store the ips to an array when I have decided they are banned?
 
Wimnat said:
I've always found IP banning pretty pointless. As mentioned you can just use a proxy to get around it or even spoof your IP (not difficult).

A much better way of handling users is to ensure people have to sign-up to post and then you can ban specific users, not just their IP.
Ah, but that opens up a whole new kettle of fish, and I have no idea at all how I would manage a registration script, that checks for passwords etc
 
Well I have an idea of how to do it, but my php skills are very little atm, so im not certain, btw what does IIRC stand for?
 
Just before I tackle this im getting on with sorting my site out, im trying to make it so that if there are >6 posts on my main page, it creates another page to visit, like in my guestbook, but I cant get it working, this is the code I have so far...

*EDIT* Nm Sorted, Back to password system!
 
Last edited:
Code:
				<?php
					// ============
					// Show entries
					// ============

					// how many entries to show per page
					$rowsPerPage = 6;

					// by default we show first page
					$pageNum = 1;

					// if $_GET['page'] defined, use the value as page number
					if(isset($_GET['page']))
					{
						$pageNum = $_GET['page'];
					}

					// counting the offset ( where to start fetching the entries )
					$offset = ($pageNum - 1) * $rowsPerPage;

					// prepare the query string
					$query = "SELECT id, title, post, date ".
         				"FROM jcb_mainMenu ".
					"WHERE sectionID = 'About Me'".
		 			"ORDER BY id DESC ".            // using ORDER BY to show the most current entry first
		 			"LIMIT $offset, $rowsPerPage";  // LIMIT is the core of paging

					// execute the query 
					$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());

					// if the page is empty show a message
					if(mysql_num_rows($result) == 0)
					{
					?>
        				<p>This Page Is Empty!</p>
    				<?php
					}
					else
					{
						// get all entries
						while($row = mysql_fetch_array($result))
					{
					// list() is a convenient way of assign a list of variables
					// from an array values 
					list($id, $title, $post, $date) = $row;
				?>     										
					<span class="mainMenu"><?=$post;?><br /><br /></span>					
					<hr/>
				<?php
					} // end while

					// below is the code needed to show page numbers

					// count how many rows we have in database
					$query   = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
					$result  = mysql_query($query) or die('Error, query failed. ' . mysql_error());
					$row     = mysql_fetch_array($result, MYSQL_ASSOC);
					$numrows = $row['numrows'];

					// how many pages we have when using paging?
					$maxPage  = ceil($numrows/$rowsPerPage);
					$nextLink = '';

					// show the link to more pages ONLY IF there are 
					// more than one page
					if($maxPage > 1)
					{
						// this page's path
						$self     = $_SERVER['PHP_SELF'];
	
						// we save each link in this array
						$nextLink = array();
	
						// create the link to browse from page 1 to page $maxPage
						for($page = 1; $page <= $maxPage; $page++)
						{
							$nextLink[] =  "<a href=\"$self?page=$page\">$page</a>";
						}
	
						// join all the link using implode() 
						$nextLink = "Page : " . implode(' &raquo; ', $nextLink);
					}

					// close the database connection since
					// we no longer need it
					?>
    					<p align="center"><?=$nextLink;?></p>
    				<?php
					}
				?>

The above makes it so if there are to many posts, then a new page is created, but its creating a second page no matter if there are no posts... Anyway to stop this?

*EDIT* Actualy it is creating numbers of pages in relation to how many posts are in the table, not how many posts are in the section e.g. blog, main menu etc...
 
Last edited:
Clarkey said:
// count how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


mysql_num_rows will do that, iirc.
Is there anyway to make it only count the number of things in a particular section?
 
robmiller said:
Code:
SELECT COUNT(*) FROM table WHERE [conditions]
Thanks :)

Code:
$query   = "SELECT COUNT(*) AS numrows FROM jcb_mainMenu WHERE sectionID = 'Main Menu'";

That leaves me with 2 more problems I need to get sorted

1) I want my guestbook to set a cookie to a user to stop them posting again, it does this with my js, however as soon as I add my style switcher js to the page it breaks...

2) I need to be able to set it so if a word is over 60 characters in lenght a space is added, e.g "AAAAAAAAAAAAAAAAAAAAAAAAAA" could become "AAAAAAAAA AAAAAAAAAA" but AAAAAAA AAAAAAA would remain as it is, with no extra space...
 
Last edited:
http://jcb33.co.uk/guestbook.php?page=1

When I enter text to long it breaks my page, is there anyway to make it so it spaces, but so that it does not count spaces as characters?

Code:
					//Adds Spacing To Stop Page Breaking	
					$title = chunk_split($title,40," ");
					$user = chunk_split($user,40," ");
					$message = chunk_split($message,40," ");

Putting the above works, but it counts spaces so you can end up with a random space in the middle of a word e.g the frog went to the zoo on mo nday
 
Last edited:
Back
Top Bottom