分页和 url 编码帮助

发布于 2024-10-09 09:41:22 字数 1238 浏览 0 评论 0原文

<?php $name=$_POST['name']; ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO" name="submit">
</form>

<?php

    include ('db.php');

    if(isset($_POST['submit'])) 
    {
    mysql_query ("INSERT INTO example (name) VALUES('$name')") or die(mysql_error());
    }

    if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
    $startrow = 0;
    } 
    else {
    $startrow = (int)$_GET['startrow'];
    }

$query = "SELECT * FROM example ORDER BY id DESC LIMIT $startrow, 20"; 

$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo "<li>";
    echo $row['name'] ." "." <a href= 'like.php?quote=" . urlencode( $row['name'] ) . "'>Click Here</a>";
    echo "</li>";
}




    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>';
?>

我想让我的页面链接隐藏,我怎样才能隐藏,以便用户无法编辑它。

第二个问题,

目前我在每页上显示总共 10 条记录,然后显示下一页按钮,但即使没有更多记录,下一页按钮也会继续显示...!如何在记录结束时删除下一页按钮。 ??

第 28 行是任何用户都可以轻松编辑的页面的链接,我希望确保它们的安全(使用 ID)

,第 35 行是下一页链接,当记录数结束时,不应出现此链接

<?php $name=$_POST['name']; ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO" name="submit">
</form>

<?php

    include ('db.php');

    if(isset($_POST['submit'])) 
    {
    mysql_query ("INSERT INTO example (name) VALUES('$name')") or die(mysql_error());
    }

    if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
    $startrow = 0;
    } 
    else {
    $startrow = (int)$_GET['startrow'];
    }

$query = "SELECT * FROM example ORDER BY id DESC LIMIT $startrow, 20"; 

$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo "<li>";
    echo $row['name'] ." "." <a href= 'like.php?quote=" . urlencode( $row['name'] ) . "'>Click Here</a>";
    echo "</li>";
}




    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>';
?>

I want to make my page links hidden , how can i make then hidden so that a user cant edit it.

2nd question,

currently i am showing total 10 records on each page and then a next page button , but the next button is keep showing even when there is no more records...! how to remove a next page button when records ended. ??

line number 28 is the link to pages which can be easyily editable by any user, i wnat to make them secure (using ID)

and line 35 is n'next' page link , this link should not be appear when number of records ended

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-10-16 09:41:22

我想不出为什么你真的应该在链接中向用户隐藏页码的原因。将它们作为 $_GET 变量保留在查询字符串中可能是我所知道的在这种特定分页情况下最常见的做法。

我会对 $_GET 变量中收到的数字进行验证,因为这通常会导致 SQL 注入 和其他问题...确保它是一个数字,可能可以被 10 整除(如果您喜欢网站的样子),也许不大于某个定义的数字,等等...

如果你真的仍然不同意,并且你想隐藏它,那么你总是可以通过在用户的计算机上保存cookie(这仍然以某种方式暴露给用户)或在会话中保存页码来做到这一点(尽管对我来说这似乎是对服务器资源的巨大浪费!)。

关于你的第二个问题 - 这有很多可能性......
这是一种方法:
创建一个 SQL 查询来查询表中有多少行。
假设数字是 55。您将其放入隐藏值中。
如果您在一页上显示 10 个项目,那么您知道最后一页是第 6 页(如果您从第 1 页开始计数,则显示第 50-55 个项目)。
页面加载时进行简单的 php 检查: if ($_GET['page'] == 5) 然后就不会显示下一个按钮。

像这样的东西(跳过验证检查和sql查询):

<input type="hidden" value="<?php echo $itemCount;?>">
<?php
if ($_GET['page'] < ($itemCount \ 10))
{
    echo "<a href=\"items.php?page=".($_GET['page']+1)."\">";
}
?>

使用这个,我会添加一个检查以确保用户不会输入大于这个数字的数字,如果他们这样做,只需将它们重定向到他们可以的最后一个号码。

I can't think of a reason why you really should hide the page numbers from the user in the link. Keeping them in the query string as $_GET variables is probably the most common practice i know of in this specific case of paging.

I would do validation on the numebrs being recieved in the $_GET variables, since this could often lead to SQL Injection and other problems... Make sure it's a number, possibly divisible by 10 (if that's how you like the site to be), perhaps not bigger than a certain defined number, etc...

If you REALLY still don't agree, and you want to hide it, then you could always do that by saving cookie on the user's computer (this is still exposed to user in some way) or save the page number in the session (although this seems like a big waste of server resources to me!).

About your second question - There are so many possibilities to this...
Here's one way :
Create an sql query that queries how many rows are there to your table.
Let's say the number is 55. You put that into a hidden value.
If you're displaying 10 items on a page then you know the last page is number 6 (showing items 50-55, if you start counting at page number 1).
Simple php check when page loads: if ($_GET['page'] == 5) then you don't display the next button.

something like this (skipping out validation checks and the sql query) :

<input type="hidden" value="<?php echo $itemCount;?>">
<?php
if ($_GET['page'] < ($itemCount \ 10))
{
    echo "<a href=\"items.php?page=".($_GET['page']+1)."\">";
}
?>

Using this, I would add a check to make sure the user doesn't enter a number bigger than this number as well, and if they do, just redirect them to the last number they can.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文