Question

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

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

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:..."

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?

Was it helpful?

Solution

You can save the variable in a session.

In your index.php file.

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

And then in your following files you can start the 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: BTW this is sort of guessing since I don't know your code or skill level.

Lets assume you have this index.php page. Which you access by 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

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

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

And fetch your link by using the $_GET variable

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

OTHER TIPS

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.

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:

< a href="profile.php?user=< ?=$this_user? >" >Profile< /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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top