PHP_SELF Error

Hi Guys,

I’m receiving this error on my php code. Can you please help me find out what is my mistake? Thanks so much.
Notice: Undefined variable: _PHP_SELF in C:\xampp\htdocs\marketing\page.php on line 67

<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<font face="Arial">
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 5;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('marketing');
/* Get total number of records */
$sql = "SELECT count(test_id) FROM tests";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT * ".
       "FROM tests ".
	   "ORDER BY test_name ".
       "LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "TEST ID :{$row['test_id']}  <br> ".
         "TEST NAME : {$row['test_name']} <br> ".
         "TEST PRICE : {$row['test_price']} <br> ".
         "--------------------------------<br>";
} 

if( $page > 0 )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous</a> |";
   echo "<a href=\"$_PHP_SELF?page=$page\">Next</a>";
}
else if( $page == 0 )
{
   echo "<a href=\"$_PHP_SELF?page=$page\">Next</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous</a>";
}
mysql_close($conn);
?>

It used to be PHP_SELF and you should use $_SERVER['PHP_SELF'] nowadays, I am not even sure if PHP_SELF exists anymore. And to prevent XSS attacks you should use

echo htmlspecialchars($_SERVER['PHP_SELF']) . '?page=' . $last;

About the XSS read this post: http://php.net/manual/en/reserved.variables.server.php#89567

Hi TeNDoLLA,

Thanks for that, I have tried changing the PHP_SELF to $_SERVER['PHP_SELF] however the error is still the same. Can you check if I did it correctly? thanks. Sorry Newbie here,.

‘“<”$_SERVER[‘PHP_SELF’]?page=$last">Previous;’

Try this which will display a list of all the $_SERVER parameters:

<pre>
	<?php print_r($_SERVER);?>
</pre>

PHPSELF Should be `PHP_SELF’

Edit:

// Instead of this:
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous</a> |";

// try this:
   echo '<a href="?page=' .$last .'">Previous</a> |"'; 

Hi John,

Thanks for the alternative tag, works almost perfectly but the thing is it’s not going through the next page. Just the first 2 pages. Can you take a look at this? thank you very much.

/*if( $page > 0 )
{  
   $last = $page - 2;
   echo "<a href=\"?page=' .$last. '\">Previous</a> | ";
   echo "<a href=\"?page' .$page. '\">Next</a>";
}
else if( $page == 0 )
{
	echo "<a href=\"?page='.$page.'\">Next</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"?page='.$last.'\">Previous</a>";
}*/

Try inserting the following to display your variables and parameters:

// Set defaults
$page   = 0;
$offset = 0;
if( isset($_GET['page'] ) )
{
   $page   = $_GET['page'] + 1;
   $offset = $rec_limit * $page ;
}
echo '<br />' .__LINE__ .': ' .$page;
echo '<br />' .__LINE__ .': ' .$offset;

$left_rec = $rec_count - ($page * $rec_limit);
echo '<br />' .__LINE__ .': ' .$left_rec;

$sql = "SELECT * ".
       "FROM tests ".
	     "ORDER BY test_name ".
       "LIMIT $offset, $rec_limit";
echo '<br />' .__LINE__ .': ' .$sql; 

$retval = mysql_query( $sql );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "TEST ID :{$row['test_id']}  <br> ".
         "TEST NAME : {$row['test_name']} <br> ".
         "TEST PRICE : {$row['test_price']} <br> ".
         "--------------------------------<br>";
} 
die; // REMARK to continue running script

Hi John thanks for your efforts, I just removed the $_PHP_SELF and it works. But the problem now is the loop is not stopping, even if there’s no record to be displayed, the Previous and Next link is still there.

// syntax incorrect and may be causing your looping problem.  
  $page = $_GET{'page'} + 1;

Try inserting these lines at the top of your page to display errors.

<?php 
error_reporting(-1); // set to maximum
ini_set('display_errors', true);

There is no error actually here, but the thing is even though there’s no more data to be displayed, the
< Previous and Next > link is still there. Thank you for monitoring this with me.

if( $page > 0 )
{
$last = $page - 2;
echo “< a href="?page=$last">< Previous< /a >”;
echo “< a href="?page=$page">Next >< /a >”;
}
else if( $page == 0 )
{
echo “< a href="?page=$page">Next ></ a>”;

}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo “< a href="?page=$last">< Previous< /a>”;
}

Try running your script then insert the following lines to inspect $sql that is causing the problem?

// remove rem after selecting Next and/or Previous then refresh the page.
// echo $sql; die;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.