PHP 链接到个人资料页面

标签 php mysql

我是 php 新手,所以我需要以下代码的一些建议:

<?php
// Start the session (pretty important!)
session_start();

// Establish a link to the database
$dbLink = mysql_connect('', '', '');
if (!$dbLink) die('Can\'t establish a connection to the database: ' . mysql_error());

$dbSelected = mysql_select_db('', $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());

$isUserLoggedIn = false;  
$query = 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';  
$userResult = mysql_query($query);  
if(mysql_num_rows($userResult) == 1) {  
    $_SESSION['user'] = mysql_fetch_assoc($userResult);  
    $isUserLoggedIn = true;  
} else {  
    if(basename($_SERVER['PHP_SELF']) != 'conectare.php') {  
        header('Location: conectare.php');  
        exit;  
    }  
}  
?>

上层代码验证用户是否登录..

我需要创建一个配置文件链接,如下所示:

<强> http://site.com/profile.php?name=NAME-OF-USER

有人可以给我一个想法吗?

我是 php 新手,所以请理解我..

PS:请不要告诉我使用 mysql、pdo 等,我已经知道它们的好处,我只需要我的代码的答案..

谢谢!

最佳答案

你只需要使用 get 变量

创建将像这样被点击的链接

将在主页或任何其他页面上单击的链接

 <?php
 $username='test';//the variable containing the username
 echo'<a href="mysite.com/profile.php?user='.$username.'">
      The link redirecting to profile page
      </a>';
 ?>

地址栏将变成这样的内容 www.mysite.com/profile.php?user=test

然后在个人资料页面上

<?php
$username_selector=$_GET['user']//in this case the value got from the link clicked is test
//then just select the necessary data using the variable storing the value got from th link clicked
?>

关于PHP 链接到个人资料页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579295/

相关文章:

php - 具有连接表数据的 Laravel selectRaw

mysql - 使用 JOIN 查询花费太多时间

php - 请比较php中的两个变量

mysql - 如何根据单独行中另一个字段的值显示一行?

php - angularjs $scope不在html View 中显示数据,但数据显示在控制台中

php - Zend_Form + DHTML 内容

php - Memcached 不监听过期时间

php - Facebook 上的链接功能如何运作?

php - 正则表达式匹配变量或字符串的值(带或不带引号)

php - move_uploaded_file() 在我的情况下不起作用