Member Avatar for hmeco

Hi Lads
im writhing a login system for a personal project.

the login part work nice but there are some includement stuff i cannot get figured out.

when a user logs in the session will start and he will be redirected to his CP.

now. the link to the cp is default.php?id=user
and in that site i want to :

include ($_SERVER['DOCUMENT_ROOT'] . '/pages/user/userinfo.php?user=' . $user );

and i get the error:

Warning: include(/home8c/sub012/sc72820-GSDQ/ocbot.org/pages/user/userinfo.php?user=admin) [function.include]: failed to open stream: No such file or directory in /mounted-storage/home8c/sub012/sc72820-GSDQ/ocbot.org/pages/user/user.php on line 9

Warning: include() [function.include]: Failed opening '/home8c/sub012/sc72820-GSDQ/ocbot.org/pages/user/userinfo.php?user=admin' for inclusion (include_path='.:/usr/share/php5/') in /mounted-storage/home8c/sub012/sc72820-GSDQ/ocbot.org/pages/user/user.php on line 9

but if i go to /pages/user/userinfo.php?user=admin then it opens fine and tells me the info in the page.

i think that whenever i include my page there it includes it BUT it does not send the user part and it expects that there is a physical file called .php?user=admin

is there a way of fixing it?

best regards
hmeco

Member Avatar for hmeco

You should provide the full path for include to work with GET variables.

include("http://yourhost.com/folder/file.php?user=someuser");

nope thats not it.

the reason why i couldent do it, its becuse i cannot declare a function in a function.

and since include "requires" that the file have to exisit in case you want to include it.

my file userinfo.php is based on dynamic content. so if i want to include a file that requires a get decleration then i will need to redeclare it before i include.. sounds wierd but true.

the solution to this issue is verry simple.

$user = $session->username; // who am i? who is the user that has logged in
$_GET['user'] = $session->username; // setting the user vareiable and packing it in GET 
include ($_SERVER['DOCUMENT_ROOT'] . '/pages/user/userinfo.php'); //viola! include this file and declare who ?user=$user is becore including :)

Here is an example of what I am talking about.

//include_example.php
include("http://localhost/test/include_example1.php?user=test"); //works, will print Welcome test
include("include_example1.php"); //works, will print Welcome
include("include_example1.php?user=test"); //won't work, will generate a warning
//include_example1.php
<?php
echo "Welcome {$_REQUEST['user']}";
?>

And, we *can* declare a function inside a function. Here is an example.

<?php
//include_example.php
include("http://localhost/test/include_example1.php?user=Will");
?>
//include_example1.php
<?php
function attachMr($username) {
	$username = "Mr. ".$username;
	function attachLastName($username) {
		$username = $username." Smith";
		return $username;
	}
	$username = attachLastName($username);
	return $username;
}
echo "Welcome ".attachMr($_REQUEST['user']);
?>

But yep, If you solved your problem, congrats :)

Cheers!
Nav

I dont post the url including a user name
store the logged in details in the session
& use $session in declarations, so there is one less opening
and the session varaible is always available

Member Avatar for hmeco

ahh i see your point
Nav.

but in your case it will require that i use 2 more files than i do allready.

but thanks for your exsample.

- hmeco

I dont post the url including a user name
store the logged in details in the session
& use $session in declarations, so there is one less opening
and the session varaible is always available

Yep. Probably that would be the best way.

But, for example, if a guest user wants to check the profile of, say Y, then, session has to be started and destroyed unnecessarily.
IMO, It really depends on the type of the application the user is developing [and I can't remember, but someone pointed me sometime ago to use sessions if only necessary].

ahh i see your point
Nav.

but in your case it will require that i use 2 more files than i do allready.

but thanks for your exsample.

- hmeco

Cheers man :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.