Page 1 of 1

help - setting input box value to variable

Posted: Tue Mar 02, 2010 6:33 pm
by bird73
Hi,

I have a set of html and php pages which requires a user to login with a valid username and password before they are brought to a data entry screen. Once logged in a session is registered for the data entry page, and the following code is at the begining of the page:

Code: Select all

 
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.html");
}
?>
 
What I would like to do is use the 'myusername' variable to populate the value of an input box later on in the page within a form that is posted to an SQL database. Doing this will allow me to track who is entering the data. I had the following code working under a webhost that I was testing, but when I switched hosts, the same code no longer works. I'm hoping someone can help as I'm pretty new to all of this.

The code that did work is:

Code: Select all

 
<label for ="loginname" style = "font-weight: bold;"> Currently logged in as:  </label>
<input type = "text" name = "loginname"  value = "<?php echo $myusername?>"  style="background-color:transparent; border:0px solid white; color:#0000FF; font-weight:bold;" readonly = "readonly">
 
Any help that could be provided would be greatly appreciated.

Thanks very much,
Kev

Re: help - setting input box value to variable

Posted: Tue Mar 02, 2010 6:53 pm
by requinix
1. session_is_registered is dead. Use isset($_SESSION["variable"]).
2. Just because something exists in the session does not mean it will automatically exist as a variable in your code. As the above suggests, use $_SESSION["variable"].

Re: help - setting input box value to variable

Posted: Tue Mar 02, 2010 7:04 pm
by bird73
tasairis, you're awsome, thank you!

I changed the session code to:

Code: Select all

<?php
session_start();
if(!isset($_SESSION["myusername"]))
{
header("location:main_login.html");
}
?>
and the input box code to:

Code: Select all

<input type = "text" name = "loginname"  value = "<?php echo $_SESSION["myusername"]?>"  style="background-color:transparent; border:0px solid white; color:#0000FF; font-weight:bold;" readonly = "readonly">
 
I'm not sure if that's exactly what you had in mind, but it appears to work. Thanks so much for the help!

Cheers,
Kev