Jump to content

why i have to log in again to see pagination results


johnvj

Recommended Posts

<html>
<body>
 
<form method="post" action="">
 
Username:<input type="text" name="user">
password:<input type="text" name="pass">
 
<input type="submit" name="submit" value="submit">
 
</form>
 
</body>
 
</html>
 
 
 
 
<?php
 
 
$user=$_POST['user'];
$pass=$_POST['pass'];
 
 
 
$conn = mysql_connect("localhost","root","root");
if ($conn):
$selectDB=mysql_select_db("project",$conn);
if (!$selectDB):
echo mysql_error();
endif;
endif;
 
// how many rows to show per page
$rowsPerPage = 2;
 
// by default we show first page
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
 
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
 
$query  = "SELECT * FROM form1 LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die(mysql_error());
 
if(isset($_POST['submit'])){
if($user=="jishil" && $pass=="1234"){
 
// print the random numbers
echo "FORM 1";
 
echo "<table width='50%' border='1'>";
echo "<tr><td>USERNAME</td><td>ADDRESS</td><td>COMPANY</td><td>SALARY</td><td>D.O.B</td><td>D.O.J</td></tr>";
 
while($rows=mysql_fetch_array($result)){
 
 
echo "<form action='' method='post'>";
 
echo "<tr>";
 
echo "<td>" . "<input type='text' name='Name' value=" . $rows['name']. " </td>";
 
echo "<td>" . "<input type='text' name='Address' value=" . $rows['address']. " </td>";
 
echo "<td>" . "<input type='text' name='Comp' value=" . $rows['comp']. " </td>";
 
echo "<td>" . "<input type='text' name='Sal' value=" . $rows['sal']. " </td>";
echo "<td>" . "<input type='text' name='Birth' value=" . $rows['birth']. " </td>";
echo "<td>" . "<input type='text' name='Join' value=" . $rows['join']. " </td>";
 
 
 
 
echo "<td>" . "<input type='hidden' name='hidden' value=" . $rows['name']. " </td>";
 
echo "<td>" . "<input type='submit' name='update' value=update" ." </td>";
 
 
echo "</tr>";
 
echo "</form>";
 
 
}
 
 
echo "</table>";
 
// how many rows we have in database
$query   = "SELECT COUNT(name) AS numrows FROM form1";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
 
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
 
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page ";   // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
 
// creating previous and next link
// plus the link to go straight to
// the first and last page
 
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";

else
{
$prev  = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
 
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";

else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
 
// print the navigation link
echo $first . $prev . $nav . $next . $last;
 
 
}}
 
 
 
?>
Edited by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

$_POST data is not remembered during multiple page requests. The post data will only exist when the form is submitted. When you click one of the pagination links, the post data will not exits and so the login form appears. The next page will only display when you filll the login form again.

 

What you should do set a login token in the $_SESSION when you authenticate the user. Then when you go to display the paginated results, check to make sure the login token exists to verify the user is authenticated. if no login token then display the login form.

 

This is how your code should be structured

<?php
// create a session, so the $_SESSION vars are remembered
session_start();

// do the login, when login form is submitted
if(isset($_POST['submit']))
{
	$user=$_POST['user'];
	$pass=$_POST['pass'];

	if($user=="jishil" && $pass=="1234")
	{
		// set sessions vars when user is authenticated
		$_SESSION['loggedIn'] = true;  // set the loggedIn token to true
		$_SESSION['username'] = $user; // the users username
	}
}
 
// only display the login form if the user is not authenticated (loggedIn token is not true)
if(!isset($_SESSION['loggedIn']) || $_SESSION['loggedIn'] !== true)
{
    // display login form
}

else
{
	// // user is authenticated display the paginated result
}

?>
Edited by Ch0cu3r
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.