简体   繁体   English

在这种情况下,我可以获取此变量值吗? (PHP)

[英]Can i get this variable value in this case? (php)

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']; 我的问题是,是否可以将变量传递给index.php,然后about.php,profile.php也可以使用$ _GET ['user']检索变量; method??(actually i tried, its failed, give me "undefined index:..." 方法?(实际上我尝试过,它失败了,给我“未定义的索引:...”

If this method failed, is there any other way? 如果此方法失败,还有其他方法吗?

EDIT: yeah, i can use SESSION. 编辑:是的,我可以使用SESSION。 How about I want to display others profile? 我要显示其他个人资料怎么样? means other username? 是指其他用户名?

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:顺便说一句,这有点猜测,因为我不知道您的代码或技能水平。

Lets assume you have this index.php page. 假设您有此index.php页面。 Which you access by index.php?user=john 您通过index.php访问哪个?user = 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>
?>

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?用户=彼得

profile.php?user=olafur profile.php?用户=奥拉维尔埃

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"]; 如果用户以index.php?user = peter开头,则只需将其另存为$ this_user = $ _GET [“ user”]; and inside index.php, when you render the links you just assign the $this_user variable, something like: 在index.php内,渲染链接时,您只需分配$ this_user变量,如下所示:

< a href="profile.php?user=< ?=$this_user? >" >Profile< /a > <a href =“ profile.php?user = <?= $ this_user?>”>个人资料</ a>

i hope this helps. 我希望这有帮助。

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. 在这种情况下,最好的解决方案是将用户名(甚至更好的是用户ID)作为参数传递给每个页面。

If you find it too hard, try looking up some PHP MVC Framework (there are lots out there). 如果您觉得它太难了,请尝试查找一些PHP MVC Framework(那里有很多东西)。 It is better than trying to roll your own. 这比尝试自己动手做得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM