Facebook喜欢自定义配置文件URL PHP [英] Facebook Like Custom Profile URL PHP

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

问题描述

制作网站,我想为我网站上的所有用户(如Facebook)添加自定义个人资料网址。



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



但是,我想为那些与他们的用户名例如,如果您转到 http://sitename.com/profile.php?id=100224232 它重定向到您 http://sitename.com/myprofile



如何使用PHP和Apache进行此操作?

解决方案

没有文件夹,没有index.php



只需看看这个教程



编辑:
这只是一个摘要。



0)上下文



我假设我们想要以下URL:


http://example.com/profile/userid (通过ID获取个人资料)

http://example.com/profile/username (按用户名获取个人资料)< br>
http://example.com/myprofile (获取当前登录的用户的配置文件)




1).htaccess



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

 选项+ FollowSymLinks 
#打开RewriteEngine
RewriteEngine On
#Rules
RewriteCond% {REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。*)$ /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 = explode('/',$ _ SERVER ['SCRIPT_NAME']); ($ i = 0; $ i< sizeof($ scriptName); $ i ++)
{
if($ requestURI [$ i] == $ scriptName [ $ i])
{
unset($ requestURI [$ i]);
}
}

$ command = array_values($ requestURI);

使用网址 http://example.com/profile/19837 ,$命令将包含:

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

现在,我们必须调度URL。我们在index.php中添加:

  // index.php 

require_once(profile.php); //我们需要这个文件
switch($ command [0])
{
case'profile':
//我们从profile.php文件运行配置文件功能。
profile($ command([1]);
break;
case'myprofile':
//我们从profile.php文件运行myProfile函数
myProfile();
break;
默认值:
//错误页面您也可以重定向到您的自定义404页面
echo404错误:错误页面;
break;
}



2)profile.php



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

  // profile.php 

函数配置文件($ chars)
{
//我们检查$ chars是否为整数(即​​一个ID)或一个字符串(即潜在的用户名)

if(is_int($ chars)){
$ id = $ chars;
//使SQL从他的ID
// ........
} else {
$ username = mysqli_real_escape_string($ char) ;
//使SQL从用户名
// ...........
}

//渲染您的视图与$用户变量
// .........
}

函数myProfile()
{
//从会话中获取当前登录的用户ID:
$ id = ....

//运行以上函数:
profile($ id);
}



总结




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喜欢自定义配置文件URL PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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