Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Page count problem...

Featured Replies

Hi!

 

Im trying to get my page count working, but I have hit a brick wall with it...

 

I get:

 

Notice: Undefined index: sort in /home/test/html/gallerypage_insert.php on line 5

 

 

I have gallery.php?name=66 in the address bar which shows only one image and previous/next buttons, which is great but

 

with error:

 

Notice: Undefined variable: sort in /home/test/html/gallerypage_insert.php on line 70

 

but when clicked on next or previous the address bar changes to

 

 

 

gallery.php?s=1&np=2&sort=

 

 

Notice: Undefined index: name in /home/test/html/gallerypage_insert.php on line 4

 

 

I know its not calling name=66 but not sure where to call the 'name'?

 

Hope this make sense.

 

Thanks B

 

<?php 

error_reporting (E_ALL);

$page_title = 'gallery'; 
	require_once ('../../mysql_connect.php'); // Connect to the db. 

$name = stripslashes($_GET['name']); 

$file=$_GET['sort']; 

$display = 1;



// if selected
if($name){ 

  $query = "SELECT * FROM `gallery` WHERE `user` = '$name'";
  $result = @mysql_query ($query); // Run the query. 
  $num = mysql_num_rows($result); 
  $myrow = mysql_fetch_array($result, MYSQL_ASSOC); 

}	  
// Determine how many pages there are. 
if (isset($_GET['np'])) { // Already been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.

	// Count the number of records
$query = "SELECT COUNT(*) FROM `gallery` WHERE `user` = '$name'";
$result = @mysql_query ($query);
$myrow = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $myrow[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
	$num_pages = ceil ($num_records/$display);
} else {
	$num_pages = 1;
}

} // End of np IF.


// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}



echo '<h3>gallery</h3> '; 

echo '<h4>'.$name.'</h4> '; 

echo '<p>Browse through the photographs by simply clicking on the numbers below which will change the main image photograph.</p>';


  /// selected everything from db 
  $query = "SELECT * FROM `gallery` WHERE `user` = '$name' LIMIT $start, $display "; 
  $result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query. 
  $num = mysql_num_rows($result);  

  if ($num > 0) { // If it ran OK, display the records. second if 


  while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) { 


  echo '<img src="uploads/'.$myrow['file1'] .' "width="500"" "height="300"" "id="main" "/>';
 } 

} else { 
   /// if db not run display error 
echo '<span class = "style1">Sorry, no records found!</span>';


}// close if statement 

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<p class="alertHdcentered"><span>';
// Determine what page the script is on.	
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
	echo '<a href="gallery.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
	if ($i != $current_page) {
		echo '<a href="gallery.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
	} else {
		echo $i . ' ';
	}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
	echo '<a href="gallery.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
}

echo '</span></p>';

} // End of links section.
?>

the sort one is because you are asking for $_GET['sort'] when it has not been set.

 

You can bypass this by either using E_ALL^E_NOTICE as your error_reporting, or by using an isset call, for example:

$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE);

  • Author

Thanks PP, thats a cool little trick! :)

 

The first image views but when clicked on next or the page number I get

 

Notice: Undefined index: name in /public_html/site/test/html/gallerypage_insert.php on line 4

 

and no image displayed also the amount of pages displayed are wrong too?

 

So im guessing my page count bit isnt working aswell?

 

with regards to the name variable Ive even tried..

 

echo '<a href="gallery.php?name='.$name.'

s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';

 

but again get the above message?

 

 

<?php 

error_reporting (E_ALL);

$page_title = 'gallery'; 
	require_once ('../../mysql_connect.php'); // Connect to the db. 

$name = stripslashes($_GET['name']); 


$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE);

$display = 1;



// if selected
if($name){ 

  $query = "SELECT * FROM `gallery` WHERE `user` = '$name'";
  $result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query.
  $num = mysql_num_rows($result); 
  $myrow = mysql_fetch_array($result, MYSQL_ASSOC); 

}	  
// Determine how many pages there are. 
if (isset($_GET['np'])) { // Already been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.

	// Count the number of records
$query = "SELECT COUNT(*) FROM `gallery` WHERE `user` = '$name'";
$result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query.
$myrow = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $myrow[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
	$num_pages = ceil ($num_records/$display);
} else {
	$num_pages = 1;
}

} // End of np IF.


// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}



echo '<h3>gallery</h3> '; 

//echo '<h4>'.$name.'</h4> '; 

echo '<p>Browse through the photographs by simply clicking on the numbers below which will change the main image photograph.</p>';


  /// selected everything from db 
  $query = "SELECT * FROM `gallery` WHERE `user` = '$name' LIMIT $start, $display "; 
  $result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query. 
  $num = mysql_num_rows($result);  

  if ($num > 0) { // If it ran OK, display the records. second if 


  while ($myrow = mysql_fetch_array($result, MYSQL_ASSOC)) { 


  echo '<img src="uploads/'.$myrow['file1'] .' "width="500"" "height="300"" "id="main" "/>';
 } 

} else { 
   /// if db not run display error 
echo '<span class = "style1">Sorry, no records found!</span>';


}// close if statement 



// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<p class="alertHdcentered"><span>';
// Determine what page the script is on.	
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
	echo '<a href="gallery.php?&s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
	if ($i != $current_page) {
		echo '<a href="gallery.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
	} else {
		echo $i . ' ';
	}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
	echo '<a href="gallery.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
}

echo '</span></p>';

} // End of links section.

//?>

In between:

 

$name = stripslashes($_GET['name']);


$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE);

$display = 1;



// if selected
if($name){

     $query = "SELECT * FROM `gallery` WHERE `user` = '$name'";
     $result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query.
     $num = mysql_num_rows($result);
     $myrow = mysql_fetch_array($result, MYSQL_ASSOC);

}      

 

I'm pretty sure there's a vulnerability. You are passing the $name variable right into the database without any cleansing or real check. I don't know if in your case that makes much of a difference to the security, but still something to watch out for.

the sort one is because you are asking for $_GET['sort'] when it has not been set.

 

You can bypass this by either using E_ALL^E_NOTICE as your error_reporting, or by using an isset call, for example:

$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE);

 

 

Mind you, it's a better practice to use isset() as otherwise you might run into unexpected behaviour or in worst case security holes.

  • Author

How can I fix the vulnerability with $name = stripslashes($_GET['name']);?

 

Thanks TT could I use the isset () with the above?

 

In between:

 

CODE

$name = stripslashes($_GET['name']);

 

 

$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE);

 

$display = 1;

 

 

 

// if selected

if($name){

 

$query = "SELECT * FROM `gallery` WHERE `user` = '$name'";

$result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query.

$num = mysql_num_rows($result);

$myrow = mysql_fetch_array($result, MYSQL_ASSOC);

 

}

 

 

I'm pretty sure there's a vulnerability. You are passing the $name variable right into the database without any cleansing or real check. I don't know if in your case that makes much of a difference to the security, but still something to watch out for.

How can I fix the vulnerability with $name = stripslashes($_GET['name']);?

 

Thanks TT could I use the isset () with the above?

 

I'd restructure the code a little:

$sort = (isset($_GET['sort']) ? $_GET['sort'] : FALSE); // where are you using this variable?
$display = 1; // ...and this?



// if selected
if(isset($_GET['name'])){
// Since the code relies on the name variable to be set,
// make sure that it doesn't use it unless it actually is.

$name = stripslashes($_GET['name']);

  $query = "SELECT * FROM `gallery` WHERE `user` = '$name'";
  $result = mysql_query ($query) or die("Query error: ". mysql_error()); // Run the query.
  $num = mysql_num_rows($result);
  $myrow = mysql_fetch_array($result, MYSQL_ASSOC);

}	  
else
{
echo 'No name spesified!';
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.