亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我在使用 htaccess 和 php 開關配置 url 時感到困惑

我在使用 htaccess 和 php 開關配置 url 時感到困惑

PHP
慕仙森 2023-10-15 10:45:41
我想用常規文件編寫一個項目。索引文件有一個 php 代碼,其中 URL 中打開的所有文件都是開關。例如:索引.php<?php if(isset($_GET['page'])){   $current_page = isset($_GET['page']) ? $_GET['page'] : '';}else{    $current_page = 'index';}  $result = str_replace(".php","", $current_page);       switch($result){       case 'welcome':         include('sources/welcome.php');       break;       case 'index':         include('sources/index.php');       break;        case 'profile':         // Here is the problem. I want to make Facebook style user profile system         // But the switch can not see profile username because it is working just for page names not usernames       break;    } ?>就像index.php 文件中的代碼一樣,我使用開關來調用頁面。但當用戶打開個人資料頁面時,一切都會改變。因為我想讓會員的個人資料頁面像Facebook一樣。喜歡http://www.mywebproject.com/username我創建的 htaccess 在這里:.htaccessRewriteRule ^(.*)$ index.php?page=$1 [L,QSA]RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]我的問題是這樣的。如何在 switch 中使用成員的用戶名來調用成員的個人資料。
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我如何在 switch 中使用成員的用戶名來調用成員的個人資料,因為 $thePage 數組中沒有每個用戶名。


只需將所有內容傳遞給index.php


.htaccess:


# Activate rewrite engine

RewriteEngine on

RewriteBase /root/


# If the request is not for a valid directory, file or symlink

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-l


# Redirect all requests to index.php

RewriteRule ^(.*)$ index\.php?/$1 [QSA]

您只需將其傳遞$_REQUEST['username']給 profile.php,然后渲染您的頁面。


就像是:


索引.php


// you can do this better, this is just an example:

$request_uri = $_SERVER['REQUEST_URI'];

$params_offset = strpos($request_uri, '?');

$request_path = '';

$request_params = [];


echo 'request_uri = ', $request_uri, '<br>', PHP_EOL;


if ($params_offset > -1) {

    $request_path = substr($request_uri, 0, $params_offset);

    $params = explode('&', substr($request_uri, $params_offset + 1));

    foreach ($params as $value) {

        $key_value = explode('=', $value);

        $request_params[$key_value[0]] = $key_value[1];

    }

} else {

    $request_path = $request_uri;

}


echo 'request_path = ', $request_path, '<br>', PHP_EOL;

echo 'request_params = ', PHP_EOL; print_r($request_params);


if (preg_match('~/root/(photos|videos|albums)/([\w-]+)~', $request_uri, $match)) {

    print_r($match);

    unset($match);

    require_once('photos_videos_albums.php');

} else if (preg_match('~/root/([\w-]+)~', $request_uri, $match)) {

    $username = $match[1];

    unset($match);

    require_once('profile.php');

} else if ($request_path == '/root/') {

    echo 'HOME';

    exit();

} else {

    header('HTTP/1.0 404 Not Found');

    echo "<h1>404 Not Found</h1>";

    echo "The page that you have requested could not be found.";

}


查看完整回答
反對 回復 2023-10-15
  • 1 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號