在这种情况下,我可以得到这个变量值吗? (PHP)

时间:2022-09-16 23:10:23

I pass a variable to domain.com/index.php?user=username

我将一个变量传递给domain.com/index.php?user=username

In my page (index.php) contains others pages like about.php, profile.php, contact.php etc..

在我的页面(index.php)中包含其他页面,如about.php,profile.php,contact.php等。

My question is, is it possible that i pass variable to index.php, then about.php, profile.php can also retrieve the variable using $_GET['user']; method??(actually i tried, its failed, give me "undefined index:..."

我的问题是,有可能我将变量传递给index.php,然后是about.php,profile.php也可以使用$ _GET ['user']检索变量;方法??(实际上我试过,它失败了,给我“未定义索引:......”

If this method failed, is there any other way?

如果此方法失败,还有其他方法吗?

EDIT: yeah, i can use SESSION. How about I want to display others profile? means other username?

编辑:是的,我可以使用SESSION。我想显示其他个人资料怎么样?是指其他用户名?

3 个解决方案

#1


You can save the variable in a session.

您可以将变量保存在会话中。

In your index.php file.

在index.php文件中。

session_start();
$_SESSION["user"] = $_GET["user"];

And then in your following files you can start the session and call $_SESSION["user"]

然后在您的以下文件中,您可以启动会话并调用$ _SESSION [“user”]

EDIT: If you need to display different content that can take the same arguments, then you need to have those arguments in that url.

编辑:如果您需要显示可以采用相同参数的不同内容,那么您需要在该URL中包含这些参数。

EDIT 2: BTW this is sort of guessing since I don't know your code or skill level.

编辑2:BTW这是一种猜测,因为我不知道你的代码或技能水平。

Lets assume you have this index.php page. Which you access by index.php?user=john

让我们假设你有这个index.php页面。您可以通过index.php访问哪些用户= john

And in this page you list friends of john. And you can access their profile also by doing index.php?user=alex and index.php?user=tim

在这个页面中,您列出了约翰的朋友。你也可以通过index.php?user = alex和index.php?user = tim来访问他们的个人资料

Then you can reference the url of their friends with. (assuming you have arrays of friends in a standard mysql_fetch_* way)

然后你可以参考他们的朋友的网址。 (假设你有一个标准的mysql_fetch_ *方式的朋友数组)

<?php
    echo "<a href='index.php?user=".$friend["name"]."'>".$friend["name"]."</a>
?>

And fetch your link by using the $_GET variable

并使用$ _GET变量获取链接

<?php
    echo "<a href='index.php?user=".$_GET["user"]."'>".$_GET["user"]."</a>
?>

#2


i am assuming that you want to pass "username" as different user identities on your website so that users may be able to view their profile, like:

我假设你想在你的网站上传递“用户名”作为不同的用户身份,以便用户可以查看他们的个人资料,例如:

profile.php?user=peter

profile.php?user=olafur

is this correct? one way is sessions, but if you like, you can also just pass them as GET vars to all links inside the pages.

它是否正确?一种方式是会话,但如果你愿意,你也可以将它们作为GET变量传递给页面内的所有链接。

eg. if the user started with index.php?user=peter you just save this as $this_user = $_GET["user"]; and inside index.php, when you render the links you just assign the $this_user variable, something like:

例如。如果用户以index.php?user = peter开头,你只需将其保存为$ this_user = $ _GET [“user”];在index.php里面,当你渲染链接时,你只需要分配$ this_user变量,例如:

< a href="profile.php?user=< ?=$this_user? >" >Profile< /a >

”>个人资料

i hope this helps.

我希望这有帮助。

#3


I suggest avoid using session for passing parameters from one page to the other. In this case, the best solution would be to pass the username (or even better, the user id) to each page as an argument.

我建议避免使用session将参数从一个页面传递到另一个页面。在这种情况下,最好的解决方案是将用户名(甚至更好的用户ID)作为参数传递给每个页面。

If you find it too hard, try looking up some PHP MVC Framework (there are lots out there). It is better than trying to roll your own.

如果你发现它太难了,试着查看一些PHP MVC框架(那里有很多)。这比试图自己动手更好。

#1


You can save the variable in a session.

您可以将变量保存在会话中。

In your index.php file.

在index.php文件中。

session_start();
$_SESSION["user"] = $_GET["user"];

And then in your following files you can start the session and call $_SESSION["user"]

然后在您的以下文件中,您可以启动会话并调用$ _SESSION [“user”]

EDIT: If you need to display different content that can take the same arguments, then you need to have those arguments in that url.

编辑:如果您需要显示可以采用相同参数的不同内容,那么您需要在该URL中包含这些参数。

EDIT 2: BTW this is sort of guessing since I don't know your code or skill level.

编辑2:BTW这是一种猜测,因为我不知道你的代码或技能水平。

Lets assume you have this index.php page. Which you access by index.php?user=john

让我们假设你有这个index.php页面。您可以通过index.php访问哪些用户= john

And in this page you list friends of john. And you can access their profile also by doing index.php?user=alex and index.php?user=tim

在这个页面中,您列出了约翰的朋友。你也可以通过index.php?user = alex和index.php?user = tim来访问他们的个人资料

Then you can reference the url of their friends with. (assuming you have arrays of friends in a standard mysql_fetch_* way)

然后你可以参考他们的朋友的网址。 (假设你有一个标准的mysql_fetch_ *方式的朋友数组)

<?php
    echo "<a href='index.php?user=".$friend["name"]."'>".$friend["name"]."</a>
?>

And fetch your link by using the $_GET variable

并使用$ _GET变量获取链接

<?php
    echo "<a href='index.php?user=".$_GET["user"]."'>".$_GET["user"]."</a>
?>

#2


i am assuming that you want to pass "username" as different user identities on your website so that users may be able to view their profile, like:

我假设你想在你的网站上传递“用户名”作为不同的用户身份,以便用户可以查看他们的个人资料,例如:

profile.php?user=peter

profile.php?user=olafur

is this correct? one way is sessions, but if you like, you can also just pass them as GET vars to all links inside the pages.

它是否正确?一种方式是会话,但如果你愿意,你也可以将它们作为GET变量传递给页面内的所有链接。

eg. if the user started with index.php?user=peter you just save this as $this_user = $_GET["user"]; and inside index.php, when you render the links you just assign the $this_user variable, something like:

例如。如果用户以index.php?user = peter开头,你只需将其保存为$ this_user = $ _GET [“user”];在index.php里面,当你渲染链接时,你只需要分配$ this_user变量,例如:

< a href="profile.php?user=< ?=$this_user? >" >Profile< /a >

”>个人资料

i hope this helps.

我希望这有帮助。

#3


I suggest avoid using session for passing parameters from one page to the other. In this case, the best solution would be to pass the username (or even better, the user id) to each page as an argument.

我建议避免使用session将参数从一个页面传递到另一个页面。在这种情况下,最好的解决方案是将用户名(甚至更好的用户ID)作为参数传递给每个页面。

If you find it too hard, try looking up some PHP MVC Framework (there are lots out there). It is better than trying to roll your own.

如果你发现它太难了,试着查看一些PHP MVC框架(那里有很多)。这比试图自己动手更好。