Facebook Like 自定义个人资料 URL PHP [英] Facebook Like Custom Profile URL PHP

查看:25
本文介绍了Facebook Like 自定义个人资料 URL PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作网站,我想为我网站上的所有用户(如 Facebook)放入一个自定义配置文件 URL.

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

但是,我想为那些与其用户名相关的页面制作镜像.例如,如果您访问 http://sitename.com/profile.php?id=100224232 它重定向到您 http://sitename.com/myprofile

我将如何使用 PHP 和 Apache 执行此操作?

解决方案

没有文件夹,没有 index.php

看看这个教程.>

这只是一个总结.

0) 上下文

我假设我们需要以下网址:

<块引用>

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

1) .htaccess

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

选项 +FollowSymLinks# 打开重写引擎重写引擎开启# 规则RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^(.*)$/index.php

有什么作用?

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

2) index.php

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

在 index.php 中:

//index.php//当 index.php 不在根文件夹中,而是在某个子文件夹中时,这是必要的...//我们比较 $requestURL 和 $scriptName 以删除不合适的值$requestURI =explode(‘/’, $_SERVER[‘REQUEST_URI’]);$scriptName = expand('/',$_SERVER['SCRIPT_NAME']);for ($i= 0; $i < sizeof($scriptName); $i++){如果($requestURI[$i] == $scriptName[$i]){未设置($requestURI[$i]);}}$command = array_values($requestURI);

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

$command = 数组([0] =>'轮廓',[1] =>19837,[2] =>,)

现在,我们必须分派 URL.我们在 index.php 中添加:

//index.phprequire_once("profile.php");//我们需要这个文件开关($命令[0]){案例个人资料"://我们从 profile.php 文件中运行 profile 函数.配置文件($命令([1]);休息;案例我的个人资料"://我们从 profile.php 文件中运行 myProfile 函数.我的简历();休息;默认://错误的页面!您还可以重定向到您的自定义 404 页面.echo "404 错误:页面错误.";休息;}

2) profile.php

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

//profile.php功能简介($chars){//我们检查 $chars 是整数(即 ID)还是字符串(即潜在的用户名)如果(is_int($chars)){$id = $chars;//执行 SQL 从他的 ID 中获取 $user//......} 别的 {$username = mysqli_real_escape_string($char);//执行 SQL 从他的用户名中获取 $user//…………}//用 $user 变量渲染你的视图//... ...}函数 myProfile(){//从会话中获取当前登录的用户 ID :$id = ....//运行上面的函数:个人资料($ id);}

总结

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

Making a website and I want to put in a custom profile URL for all the users on my site (like facebook).

On my website already, people have a page like http://sitename.com/profile.php?id=100224232

However, I want to make a mirror for those pages that relates to their username. For example, if you go to http://sitename.com/profile.php?id=100224232 it redirects to you http://sitename.com/myprofile

How would I go about doing this with PHP and Apache?

解决方案

No folders, no index.php

Just take a look at this tutorial.

Edit : This is just a summary.

0) Context

I'll assume that we want the following URLs :

http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)

1) .htaccess

Create a .htaccess file in the root folder or update the existing one :

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

What does that do ?

If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.

2) index.php

Now, we want to know what action to trigger, so we need to read the URL :

In 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);

With the url http://example.com/profile/19837, $command would contain :

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

Now, we have to dispatch the URLs. We add this in the 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

Now in the profile.php file, we should have something like this :

// 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);
}

To conclude

I wish I was clear enough. I know this code is not pretty, and not in an OOP style, but it could give some ideas...

这篇关于Facebook Like 自定义个人资料 URL PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆