session help

hello guys, i am an it student and im confuse how to display the user’s firstname when he or she login using session.

here is my login page code:

[php]<?php

session_start();

$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “student”;

mysql_connect($host, $user, $pass);
mysql_select_db($db);

if (isset($_POST[‘username’])) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];
$user = $_POST[‘username’];
$pass = $_POST[‘password’];
$sql = “SELECT * FROM student WHERE username=’”.$username."’ AND password=’".$password."’ LIMIT 1";
$res = mysql_query($sql);

if (mysql_num_rows($res) == 1 ) {

$user = $_POST['username'];
$pass = $_POST['password'];
$sql2 = "select * from student where username = '".$username."' ";
$resfname = mysql_query($sql2);
$_SESSION['fname'] = $resfname;
echo "login successful.";
header("Location: sad52/studentpage.php");
exit();

}
else {

echo"invalid username or password.";
exit();
}}

?>

Student


Log in to My Account

    UserName  

    Password  

   


	<div id="column_r" style="left: -1px; top: 0px; width: 193px">
[/php]

and here is the code of the page when the user login

[php]<?php

session_start();

$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “student”;

if(isset($_POST[‘submit’]))
{

echo $_SESSION['fname']; 

}

?>

[/php]

hope you guys can help me…
all your replies will be appreciated.
thank you www.phphelp.com

Couple of things,
1 - you need to add session_start(); to the very top of the page (before any html)
2 - You need to change if (isset($_POST[‘username’])) { to if (isset($_POST[‘submit’])) { on that first page and on the hell page, get rid of it completely.
3 - for any of the php to work, you need to either remove the action, or move the processing code to the page indicated in the action.
4 - you can not have any text being printed to the screen (echo, print, etc) before a header call, you’ll get a butt load of headers already sent error messsages.
5 - if the action is removed, then you need to name your submit button.

login page
[php]session_start(); // has to be at the very top of the page
if (isset($_POST[‘submit’])) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$sql = mysql_query("SELECT * FROM student WHERE username='$username' AND password='$password'") or die mysql_error());

if (mysql_num_rows($slq) == 1 ) {
	$row = mysql_fetch_assoc($sql);
	$_SESSION['fname'] = $row['fname']; // change to match your first name column
	header("Location: sad52/studentpage.php");
}else {
	echo"invalid username or password.";
	exit();
}

}
?>[/php]
You’re already selecting all the information for that user, there’s no reason to request the same information. I could see it if you were just asking for 1 column in the first query, but you’re not. And since you’re getting the information, why not use it.

welcome page
[php]// html code here

<?=(isset($_SESSION['fname']) ? $_SESSION['name'] : "") ?>

// rest of html code[/php]

thanks for the reply richei but after the login it wont go to the welcome page … sorry im noob,

loginpage
[php]<?php

$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “student”;

mysql_connect($host, $user, $pass);
mysql_select_db($db);

session_start();
if (isset($_POST[‘submit’])) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$sql = mysql_query("SELECT * FROM student WHERE username='".$username."' AND password='".$password."' ");

if (mysql_num_rows($slq) == 1 ) {
	$row = mysql_fetch_assoc($sql);
	$_SESSION['fname'] = $row['fname']; 
	header("Location: sad52/studentpage.php");
}else {
	echo"invalid username or password.";
	exit();
}

}
?>

Student


Log in to My Account

    UserName  

    Password  

   


	<div id="column_r" style="left: -1px; top: 0px; width: 193px">
[/php]

welcomepage

[php]

<?=(isset($_SESSION['fname']) ? $_SESSION['name'] : "") ?> Welcome <? echo $_SESSION['name'] ?> [/php]

thankyou www.phphelp.com

ok, again. in your form tag, you have action=“student.php”. that tells the form to send the information in the form to that page. IF the php you have posted is on that page, then you’re good, BUT if its not, then nothing will happen. Since the code appears to be on the same page, then just change the action to action="".

hi richie I did what you said… i changed it to action="" but it didnt solved the problem, it doesnt go to the welcome page :frowning:

thankyou www.phphelp.com

you didn’t name the submit button. change to

im sorry :slight_smile:

ok i did it but i got this error now …

[u][i]Notice: Undefined variable: slq in C:\xampp\htdocs\student.php on line 19

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\student.php on line 19
invalid username or password.[/i][/u]

but the username and password I inputted are correct.

can someone help? problem still unsolved.

here is the error

[i]Notice: Undefined variable: slq in C:\xampp\htdocs\student.php on line 19

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\student.php on line 19
invalid username or password.[/i]

and here is the code:

[php]<?php

$host = “localhost”;
$user = “root”;
$pass = “”;
$db = “student”;

mysql_connect($host, $user, $pass);
mysql_select_db($db);

session_start();
if (isset($_POST[‘submit’])) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$sql = mysql_query("SELECT * FROM student WHERE username='".$username."' AND password='".$password."' ");

if (mysql_num_rows($slq) == 1 ) {
	$row = mysql_fetch_assoc($sql);
	$_SESSION['fname'] = $row['firstname']; 
	header("Location: sad52/studentpage.php");
}else {
	echo"invalid username or password.";
	exit();
}

}
?>

Student


Log in to My Account

    UserName  

    Password  

   


	<div id="column_r" style="left: -1px; top: 0px; width: 193px">
[/php]

here is the code to the welcome page

[php]

<?=(isset($_SESSION['fname']) ? $_SESSION['name'] : "") ?> Welcome <? echo $_SESSION['name'] ?> [/php]

That was my mistake, sorry about that, look at the spelling in mysql_num_rows($slq), it should be $sql.

and change the welcome page to
[php]

Welcome <?=$_SESSION['name'] ?> [/php]

thank you richei ! it now continue to the welcome page, but the firstname wont appear.

in my database it is “firstname”.

On the welcome page, add session_start(); to the top of the page.

I did it already here is the code :

[php]<? session_start(); ?>

Welcome <? echo $_SESSION['fname'] ?> [/php]

but id doesnt fix the problem yet :frowning:

but it doesnt fix the problem yet :frowning:

problem solved

in my welcome page i just changed the code into

[php]<?php
session_start();
echo "Welcome " .$_SESSION[‘fname’];

?>[/php]

thankyou www.phphelp.com

It wasn’t working because you were trying to use short tags, which is turned off by default.

Sponsor our Newsletter | Privacy Policy | Terms of Service