有个人资料页面的问题

时间:2013-05-06 20:42:23

标签: php mysql .htaccess

我正在创建个人资料页面,其中包含个人资料照片,用户名,标题以及个人资料所有者的朋友列表。

我使用 .htaccess 来允许用户在网址中写入,只是其他用户的名称,而无需使用来访问个人资料页面。

但问题是,如果我在URL中写了名字,它就可以了,但是如果我把配置文件.php或者我按下配置文件它会显示基本页面,这意味着没有任何与此配置文件所有者相关的信息,如新页面

如何解决这个问题?

profile.php

session_start();
require_once('include/connect.php'); 
$login = ($_SESSION['login']);
$userid = ($_SESSION['user_id']);
$login_user = ($_SESSION['username']);
$fname = ($_SESSION['first_name']);
$lname = ($_SESSION['last_name']);
ob_start();


$username = "";
$interactionBox = "";

if(isset($_GET['u'])) {
    $username = mysql_real_escape_string($_GET['u']);

    if(ctype_alnum($username)) {
        //check  ser exists
        $check = mysql_query("SELECT user_name, first_name FROM user WHERE user_name = '$username'");

        if(mysql_num_rows($check) == 1) {
            $get = mysql_fetch_assoc($check);
            $username = $get['user_name'];
            $fname = $get['first_name'];
            var_dump($username);
            var_dump($login_user);
        } else {
            echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/lam-el-chamel/index.php\">";
            exit();
        }
    }
}
?> 

<?php
//check if the logued in user is diffrenet from the url username 
if($username != $login_user) {
    $interactionBox='<div class = "InteractionLinksDiv">
    <a href= "#" onclick="return false" onmousedown="javascript:toggleInteractContainers(\'add_friend\');">Add as Friend</a>
    </div>';
} else { //check if the logued in user is equal to the url username
    $interactionBox='<div style="display:inline; border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:14px;">
    Others Can  Add You.
    </div>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Lam_El_Chamel</title>
    <link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css' />
    <link href='http://fonts.googleapis.com/css?family=Abel|Satisfy' rel='stylesheet' type='text/css' />
    <link href="default.css" rel="stylesheet" type="text/css" media="all" />
    <!--[if IE 6]>
    <link href="default_ie6.css" rel="stylesheet" type="text/css" />
    <![endif]-->

    <script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </script>

    <script language="javascript" type="text/javascript">
        //jquery function for toggeling member interaction container
        function toggleInteractContainers(x) {
            if($('#'+x).is(":hidden")){
                $('#'+x).slideDown(200);
            } else {
                $('#'+x).hide();
            }
        }
        //function to add friend recive 2 arguments
        function addAsFriend(a,b) {
            //alert("Member with id:" + a + "request friendship with the memeber with id:" + b);
            var url = "script_for_profile/request_as_friend.php";
            $("#add_friend").text("please wait...").show();

            $.post(url,{request:"requestFreindship",mem1:a,mem2:b},function(data){
                $("#add_friend").html(data).show().fadeOut(12000);
            });
        }
    </script>
</head>
<body>
    <?php require_once('header.php');?>
    <div id="wrapper">
        <div id="page-wrapper">
            <div id="page">
                <div id="wide-content">
                <?php

                $check_pic = mysql_query("SELECT profile_pic FROM user WHERE user_name= '$username'")or die(mysql_error());

                $get_pic_row = mysql_fetch_assoc($check_pic);
                $profile_pic_db = $get_pic_row['profile_pic'];
                if($profile_pic_db == "") {
                    $profile_pic = "images/default_img.jpg".$profile_pic_db;
                }
                else {
                    $profile_pic = "userdata/profile_pics/".$profile_pic_db;
                }
                ?> 
                <img src="<?php echo $profile_pic; ?>" height="150" width="196"  alt="<?php echo $username; ?>'s profile" title="<?php echo $username; ?>'s profile" />
                <br />
                <div class="textHeader"><?php echo $username; ?></div>
                <?php echo $interactionBox; ?>
                <div class="interactContainers" id="add_friend">
                    <div align="right"><a href="#" onclick="return false" onmousedown="javascript:toggleInteractContainers('add_friend');">Cancel</a></div>
                    Add <?php echo $username ?> as Friend?&nbsp;

                    <a href ="#" onclick="return false" onmousedown="javascript:addAsFriend(<?php echo $_SESSION['user_id']; ?>,<?php echo $userid; ?>);">Yes</a>
                </div>
                <div class="interactContainers" id="friend_requests" style="background-color:#FFF ; height:240px; overflow:auto;">
                    <h3>The Following People want to be friends</h3>
                </div>
                <div class="profileLeftSideContent">Introduce YourSelf....<br />
                <?php
                $about_query = mysql_query("SELECT interest FROM user WHERE user_name = '$username'")or die(mysql_error());
                $get_result = mysql_fetch_assoc($about_query);
                $about_the_user = $get_result['interest'];

                echo $about_the_user;
                ?>

的.htaccess

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1 [L]

1 个答案:

答案 0 :(得分:0)

您当前的RewriteRule会将请求重写为/profile.php/profile.php?u=profile.php

您应该定义前缀或sufix以检测应该重写的URL。例如

RewriteRule ^profile/([a-zA-Z0-9_-]+)$ profile.php?u=$1 [L]

然后,您就可以通过/profile/UserName访问个人资料页面了,调用/profile.php仍可以使用。