不同用户使用 PHP 的不同页面 [英] Different page for different users using PHP

查看:56
本文介绍了不同用户使用 PHP 的不同页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何让用户McKenzie"看到他自己可以操纵的独特页面,以及Wendy"在她登录时看到她自己的页面.

我已经创建了登录名和页面,将它们连接到给定 ID 等的 MySQL 数据库,所以我可以完成所有这些并且我知道会话等;)

那么有人可以告诉我我将如何做到这一点,我是否必须为每个单独的用户制作不同的页面?我在想一些事情

注册页面:

在数据库中存储数据,获取用户 ID 并使用?pageid=1"将用户带到基于 ID 的页面.

但我不确定如何在不手动制作每个页面的情况下制作它们,因为您可以想象为每个单独的用户制作一个新页面会很痛苦……而且效率很低.有任何想法吗?

请给我看一个带代码的例子,我将不胜感激!先谢谢大家!

解决方案

我的回答是假设您想要创建完全可自定义的用户数据,并增加在用户之间共享页面的可能性(如个人资料页面).有了这个,您可以通过创建一个 php 页面来实现这一点,该页面通过 $_GET $_POST 数据搜索 MySQL 表.

我会将这个答案扩展为几个步骤...

SQL 表

您需要的第一件事是您的 MySQL 设置,假设您已经完成了基本设置,但我将继续创建一个简单的设置.

基本设置将是登录数据和自定义用户数据,您可以查看我的设置 此处.

php 用户页面

最简单的方法是从 $_GET 数据中获取请求的用户.为此,我们只需获取数据并请求用户信息:

$requested_user = $_GET['id'];$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'root', 'MyPassword');尝试 {$stmt = $db->prepare("SELECT * FROM c_userpage WHERE id = ?");$stmt->execute(array($requested_user));$mydata = $stmt->fetch();} 捕获(异常 $e){//mysql 出错死();}

现在我们可以简单地将用户数据添加到页面!

echo "你好!我的名字是 {$mydata['username']}!\n";echo "关于我:{$mydata['custom_data']}";

将用户发送到他们的页面

我们可以简单地使用 www.page.com/user.php?id=2 这将请求 id=2 用户的数据>

额外内容

如果您想保持用户页面的私密性,您只需使用 $_POST$_SESSION 请求 ID,然后检查用户是否已登录!

user.php 的完整代码

带有私人页面的 user.php 的完整代码

I want to know how I would get user 'McKenzie' to see his own unique page that he can manipulate and 'Wendy' to see her own page when she logs in.

I've created the login and the pages, connected them to a MySQL database given them ID's etc, so I can do all of this and I know about sessions etc. ;)

So can someone tell me how I would do this, would I have to make different pages for each separate user? I'm thinking something along the lines of

REGISTER PAGE:

Store data in database, get user ID and use "?pageid=1" to then take the user to the id based page.

But I'm not sure how I would make each page without making them manually, as you can imagine making a new page for each separate user would be a pain... and very inefficient. Any ideas?

And please show me an example with code, it would be GREATLY appreciated! Thank you all in advance!

解决方案

My answer is assuming you want to create fully customizable user data with the added possibility of sharing the page between users (like a profile page). With that out of the way you can do this by creating one php page that searches the MySQL table by $_GET or $_POST data.

Ill expand this answer in to a couple of steps...

SQL Tables

The first thing you will need is your MySQL set-up, ill assume you have a basic set-up already done but I will go ahead and create a simple one.

The basic set-up will be the login data and the custom user data, you can view my set-up here.

php user page

The simplest way would be to get the requested user from the $_GET data. So to do this we would simply get the data and request the users information:

$requested_user = $_GET['id'];

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'root', 'MyPassword');

try {

    $stmt = $db->prepare("SELECT * FROM c_userpage WHERE id = ?");

    $stmt->execute(array($requested_user));

    $mydata = $stmt->fetch();

} catch (Exception $e) {

    //error with mysql
    die();

}

Now we can simply add the users data to the page!

echo "Hello! my name is {$mydata['username']}!\n";
echo "About Me: {$mydata['custom_data']}";

Sending users to their page

We can simply just use www.page.com/user.php?id=2 And this will request the data for the user with id=2

Extras

If you want to keep user pages private you can simply request the id with $_POST or $_SESSION and then check if the user is logged in!

Full code for user.php

Full code for user.php w/ private page

这篇关于不同用户使用 PHP 的不同页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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