Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sessions??

Status
Not open for further replies.

VisBasDude

Programmer
Nov 10, 2002
19
GB
Hi there, im relativly new to PHP but really like it and at the moment im just experimenting, currently im trying to make a login page. This is going well but now i want to have global varibles which will contain user details over the pages. I presume using sessions is the correct way to do this. The code below doesnt seem to work for me, it doesnt seem to write to the sessions, could someone please help me out.

<?
session_start();
session_register(&quot;GlobalUsername&quot;);

function CheckUsernameandPassword($Username,$Password){
$Filename = &quot;Users.txt&quot;;
$Open = fopen($Filename, &quot;r&quot;);
$Found = FALSE;
if($Open) {
$Data = file($Filename);
for ($n = 0; $n < count($Data);
$n++){
$GetData = explode(&quot;\t&quot;,$Data[$n]); // Gets data and splits it up
$FoundUsername = &quot;$GetData[0]&quot;;
$FoundPassword = &quot;$GetData[1]&quot;;
$FoundEmailAddress = &quot;$GetData[4]&quot;;
$FoundAdmin = &quot;$GetData[7]&quot;;
$FoundAdvancedUser = &quot;$GetData[8]&quot;;
$FoundDeleted = &quot;$GetData[9]&quot;;
if((&quot;$Username&quot; == &quot;$FoundUsername&quot;)&&(&quot;$Password&quot; == &quot;$FoundPassword&quot;)&&(&quot;$FoundDeleted&quot; == &quot;No&quot;)){
$Found = TRUE;
$GlobalUsername = &quot;$FoundUsername&quot;; }// I want this varible to be global

}
}
}
if (&quot;$Found&quot; == TRUE){
$CallFunction=LoginOK(); // If user found call this page
} else {
$CallFunction=LoginError();}

}
?>

<html>

<head>
<title>Logged In</title>

</head>

<body>
<?php

$CallFunction=CheckUsernameandPassword(&quot;$Username&quot;,&quot;$Password&quot;);

function LoginOK (){
print(&quot;<p align=\&quot;left\&quot;><font face=\&quot;Arial Rounded MT Bold\&quot; size=\&quot;6\&quot;>Welcome $GlobalUsername </font></p>&quot;); // Here i wanted to put the username into the text
print($GlobalUsername);
print(&quot;<p align=\&quot;left\&quot;><font face=\&quot;Arial Rounded MT Bold\&quot;>Login Ok <A HREF=\&quot; Here</A></P>&quot;);
}

function LoginError (){
print(&quot;Login Error&quot;);
}

?>

</body>
</html>

I would be very greatful if you could. Thankyou James
 
I don't think your problem is because of the session. It's because you're not using variable scoping correctly.

When you issue session_register(&quot;GlobalUserName&quot;), it's at the main scope. When you assign a value to $GlobalUserName inside your function, that particular variable is local to your function, not the variable you registered.

Issue:

global $GlobalUserName;

at the beginning of your function to make the function use the variable at the higher scope.
Want the best answers? Ask the best questions: TANSTAAFL!
 
Well ive tried what you surgested and it dont seem to have helped really, it still wont display it or move it around the site! Any other ideas?
 
As far as i can tell yeah it does, how can i confirm this though?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top