Can I get this variable value in this case? (PHP)

I am passing the variable domain.com/index.php?user=username

My page (index.php) contains other pages like about.php, profile.php, contact.php, etc.

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

If this method fails, is there any other way?

EDIT: yes, I can use SESSION. How about showing the profile of others? means a different username?

0


source to share


3 answers


You can store the variable in the session.

In your index.php file.

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

      

And then in the following files you can start a session and call $_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.

EDIT 2: By the way, this is kind of a guess, since I don't know your code or skill level.



Let's assume you have this index.php page. Who are you referring to by index.php? User = john

And on this page you will list John's friends. And can you access their profile by doing index.php as well? User = alex and index.php? User = tim

Then you can link to your friends URL. (assuming you have arrays of friends in mysql_fetch_ * standard mode)

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

      

And select your link using the $ _GET variable

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

      

+1


source


I am assuming that you want to pass "username" as different user IDs on your website so that users can view their profile, for example:

profile.php? User = Peter

profile.php? User = OLAFUR

It is right? one way is sessions, but if you like you can also just pass them as GET vars to all links within pages.



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

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

Hope this helps.

+1


source


I suggest avoiding using a session to pass parameters from one page to another. In this case, the best solution would be to pass the username (or even better, the user ID) to each page as an argument.

If you find it too complicated, try looking for some PHP MVC Framework (there are lots there ). It's better than trying to roll on your own.

0


source







All Articles