【问题标题】:php category title not showingphp类别标题不显示
【发布时间】:2019-04-22 19:17:02
【问题描述】:

我正在创建 PHP 类别页面,我的脚本没有显示 # 标记和 + 标记添加的类别标题示例,例如我的 URL Localhost

http://localhost/zblog/category/1/2/c++ 以及我的另一个标题添加了这样的类别打开http://localhost/zblog/category/1/3/php

这是我的代码

if(isset($_GET['srcid'])) {
$srcid = $_GET['srcid'];
$title= $_GET['title'];
}

分类链接

<span class="post-category"><a href="<?php echo $url; ?>
category/1/<?php echo $row['cat_id']  ?>/
<?php echo $row['cat_title']; ?>" title="View all posts in General" rel="category tag">
<?php echo $row['cat_title']; ?></a></span>

网址链接

 <a class="post-source" href="<?php echo $url; ?>
<?php echo 'category/'.rawurlencode($row['cat_id']).'/'.
rawurlencode($row['cat_title']);?>"><h1 class="post-title">
<?php echo $row['cat_title']  ?></h1></a> 

【问题讨论】:

    标签: php


    【解决方案1】:

    # 是一个不安全的 url 字符,因为它在 URL 中有意义。它被称为fragment,与链接锚一起使用。

    + 是一个安全字符,应该没有理由不工作。

    我建议您尝试以不同的方式解析您的网址。

    $url = 'http://localhost/zblog/category/1/2/c++';
    $path = parse_url($url, PHP_URL_PATH);
    $categories = explode("/", str_replace("/zblog/category/", "", $path));
    
    var_dump($categories);
    

    输出:

    array(3) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "2"
      [2]=>
      string(3) "c++"
    }
    

    【讨论】:

    • 使用非 URL 安全字符始终是个问题,容纳它们可能只是麻烦,因为某些代理可能会拒绝它们。
    【解决方案2】:

    您正在以错误的方式为 PHP 准备链接。此外,您需要 ecnode 特殊字符以通过 URL 发送。为了让 PHP 正确解释 URL 并从中获取值,您需要这样做:

    <span class="post-category"><a href="<?php echo $url; ?>?srcid=<?php echo 'category/1/'.rawurlencode($row['cat_id']).'/';  ?>&title=<?php echo rawurlencode($row['cat_title']); ?>" title="View all posts in General" rel="category tag">
    

    这是一个如何创建 PHP 链接的示例:

    http://web_adress/page.php?first_param=value_of_first_param&second_param=value_of_second_param&third_param=value_of_third_param
                              ^                                ^                                  ^
                              Here you start with params       Here you are telling the other one is coming 
    

    那么你可以使用:

    if(isset($_GET['first_param']) {
      //Get all others or whatever
      $second_param = rawurldecode($_GET['second_param']);
    
      //Your example
      $srcid = rawurldecode($_GET['srcid']);
      $title = rawurldecode($_GET['title']);
    }
    

    如果您可以将代码调整为我在此处创建的示例,它将起作用。为了能够通过 URL 发送特殊字符,请使用 rawurlencode 对这些字符进行编码,在另一端使用 rawurldecode 对其进行解码。我在上面做了一个例子。请注意,如果您使用urlencode + 符号将无法通过,您将获得空间。

    【讨论】:

    • 我添加了但结果相同我的问题是 php,html,css 之类的类别打开但 c# c++ 之类的类别未打开
    • 我已经更新了我的答案。我为特殊字符添加了编码。
    • 我改了但是结果一样
    • 有什么错误吗?您的链接现在如何显示?您能否更新您的代码,以便我查看并找到问题?
    • 我的 c# 类别 url 显示像这样 localhost/zblog/category/1/1/c%23 但不显示
    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多