/    Sign up×
Community /Pin to ProfileBookmark

Cannot echo "username" from SESSION

Hi here is the login page

[code=php]
<?
session_start();
session_register(“login_session”);
$username = “”;
$password = “”;

if (isset($_POST[“Username”]))
$username = $_POST[“Username”];
if (isset($_POST[“Password”]))
$password = $_POST[“Password”];

if ($username != “” && $password != “”) {

$link = mysql_connect(“localhost”,”root”,”root”);
mysql_select_db(“database”);

$sql = “SELECT * FROM trainee WHERE password='”;
$sql.= $password.”‘ AND username='”.$username.”‘”;
$result = mysql_query($sql);
if (mysql_fetch_row($result) != false) {

$_SESSION[“login_session”] = true;
if ($username = “trainee”){
header(“Location: student.php”);}
}else
echo ‘<script type=”text/javascript”>
window.onload=function(){alert(“It is incorrect! Please enter again!”);}
</script>’;
mysql_close($link);
}
?>
[/code]

Below is the form:

[code=html]
<div class=”sidebar”>
<div class=”sidebar_item” id=”login_form”>
<p>&nbsp;</p>
<form method=”post” action=”login_stu.php”>
<fieldset>
<legend align=”center”>Trainee Login</legend>
<p>
<label for=”ID”> Username:</label>
<input type=”text” name=”Username” />
</p>
<p>
<label for=”PASSWORD”> Password:<br />
</label>
<input type=”password” name=”Password” />
</p>
<p align=”center”>
<input type=”submit” name=”submit” value=”Login” style=”height:1.7em; width:3.5em;”/>
</p>
</fieldset>
</form>
</div>
</div>
[/code]

I got another page requiring me to display the username
And I have tried to use

[code=php]session_start();
echo $_SESSION[‘username’][/code]

or

[code=php]session_start();
echo $username;[/code]

, etc.

Any ideas? Thank you.?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@simplypixieDec 27.2012 — You aren't assigning the username to a session variable anywhere, so you need to do so
[code=php]if (mysql_fetch_row($result) != false) {

$_SESSION["login_session"] = true;
$_SESSION['username'] = $username;
[/code]
×

Success!

Help @hilda spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...