【问题标题】:Facebook Like Custom Profile URL PHPFacebook Like 自定义个人资料 URL PHP
【发布时间】:2012-05-15 06:52:06
【问题描述】:

创建一个网站,我想为我网站上的所有用户(如 facebook)输入一个自定义配置文件 URL。

在我的网站上,人们已经有了http://sitename.com/profile.php?id=100224232 之类的页面

但是,我想为那些与其用户名相关的页面创建一个镜像。例如,如果您转到http://sitename.com/profile.php?id=100224232,它会重定向到您http://sitename.com/myprofile

我将如何使用 PHP 和 Apache 来做这件事?

【问题讨论】:

    标签: php facebook profile


    【解决方案1】:

    没有文件夹,没有 index.php

    看看这个tutorial

    编辑: 这只是一个摘要。

    0) 上下文

    我假设我们需要以下 URL:

    http://example.com/profile/userid(通过 ID 获取个人资料)
    http://example.com/profile/username(通过用户名获取个人资料)
    http://example.com/myprofile(获取当前登录用户的个人资料)

    1) .htaccess

    在根文件夹中创建一个 .htaccess 文件或更新现有文件:

    Options +FollowSymLinks
    # Turn on the RewriteEngine
    RewriteEngine On
    #  Rules
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php
    

    这是做什么的?

    如果请求是针对真实目录或文件(存在于服务器上的),则不提供 index.php,否则每个 url 都会重定向到 index.php

    2) 索引.php

    现在,我们想知道要触发什么动作,所以我们需要读取 URL:

    在 index.php 中:

    // index.php    
    
    // This is necessary when index.php is not in the root folder, but in some subfolder...
    // We compare $requestURL and $scriptName to remove the inappropriate values
    $requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
    $scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
    
    for ($i= 0; $i < sizeof($scriptName); $i++)
    {
        if ($requestURI[$i] == $scriptName[$i])
        {
            unset($requestURI[$i]);
        }
    }
    
    $command = array_values($requestURI);
    

    使用 URL http://example.com/profile/19837,$command 将包含:

    $command = array(
        [0] => 'profile',
        [1] => 19837,
        [2] => ,
    )
    

    现在,我们必须发送 URL。我们在 index.php 中添加这个:

    // index.php
    
    require_once("profile.php"); // We need this file
    switch($command[0])
    {
        case ‘profile’ :
            // We run the profile function from the profile.php file.
            profile($command([1]);
            break;
        case ‘myprofile’ :
            // We run the myProfile function from the profile.php file.
            myProfile();
            break;
        default:
            // Wrong page ! You could also redirect to your custom 404 page.
            echo "404 Error : wrong page.";
            break;
    }
    

    2) profile.php

    现在在 profile.php 文件中,我们应该有这样的内容:

    // profile.php
    
    function profile($chars)
    {
        // We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
    
        if (is_int($chars)) {
            $id = $chars;
            // Do the SQL to get the $user from his ID
            // ........
        } else {
            $username = mysqli_real_escape_string($char);
            // Do the SQL to get the $user from his username
            // ...........
        }
    
        // Render your view with the $user variable
        // .........
    }
    
    function myProfile()
    {
        // Get the currently logged-in user ID from the session :
        $id = ....
    
        // Run the above function :
        profile($id);
    }
    

    总结

    我希望我足够清楚。我知道这段代码不漂亮,也不是 OOP 风格,但它可以提供一些想法......

    【讨论】:

    • 能否请您解释一下,我不太了解路线,因为我对 apache 的经验很少
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多