# session help

**URL:** <https://www.phphelp.com/t/session-help/19153>\
**Category:** General PHP Help\
**Created:** [December 17, 2012, 6:46am UTC](https://www.phphelp.com/t/session-help/19153 "2012-12-17T06:46:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 6:46am UTC](https://www.phphelp.com/t/session-help/19153/1 "2012-12-17T06:46:43Z")

</div>

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 ![]()  
  
&nbsp;&nbsp;&nbsp; UserName &nbsp;  
  
&nbsp;&nbsp;&nbsp; Password &nbsp;   
  
&nbsp;&nbsp;&nbsp;  
  
  

```
	<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](http://www.phphelp.com)

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 7:26am UTC](https://www.phphelp.com/t/session-help/19153/2 "2012-12-17T07:26:42Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 8:16am UTC](https://www.phphelp.com/t/session-help/19153/3 "2012-12-17T08:16:08Z")

</div>

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 ![]()  
  
&nbsp;&nbsp;&nbsp; UserName &nbsp;  
  
&nbsp;&nbsp;&nbsp; Password &nbsp;   
  
&nbsp;&nbsp;&nbsp;  
  
  

```
	<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](http://www.phphelp.com)

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 8:20am UTC](https://www.phphelp.com/t/session-help/19153/4 "2012-12-17T08:20:26Z")

</div>

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="".

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 8:38am UTC](https://www.phphelp.com/t/session-help/19153/5 "2012-12-17T08:38:10Z")

</div>

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 ☹

thankyou [www.phphelp.com](http://www.phphelp.com)

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 8:53am UTC](https://www.phphelp.com/t/session-help/19153/6 "2012-12-17T08:53:40Z")

</div>

you didn’t name the submit button. change to

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 9:01am UTC](https://www.phphelp.com/t/session-help/19153/7 "2012-12-17T09:01:06Z")

</div>

im sorry 🙂

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.

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 9:13am UTC](https://www.phphelp.com/t/session-help/19153/8 "2012-12-17T09:13:54Z")

</div>

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 ![]()  
  
&nbsp;&nbsp;&nbsp; UserName &nbsp;  
  
&nbsp;&nbsp;&nbsp; Password &nbsp;   
  
&nbsp;&nbsp;&nbsp;   
  
  

```
	<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]

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 9:26am UTC](https://www.phphelp.com/t/session-help/19153/9 "2012-12-17T09:26:47Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 10:37am UTC](https://www.phphelp.com/t/session-help/19153/10 "2012-12-17T10:37:02Z")

</div>

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

in my database it is “firstname”.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 11:03am UTC](https://www.phphelp.com/t/session-help/19153/11 "2012-12-17T11:03:33Z")

</div>

On the welcome page, add session\_start(); to the top of the page.

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 11:24am UTC](https://www.phphelp.com/t/session-help/19153/12 "2012-12-17T11:24:53Z")

</div>

I did it already here is the code :

[php]\<? session\_start(); ?\>

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

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 12:14pm UTC](https://www.phphelp.com/t/session-help/19153/13 "2012-12-17T12:14:12Z")

</div>

but id doesnt fix the problem yet ☹

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 12:18pm UTC](https://www.phphelp.com/t/session-help/19153/14 "2012-12-17T12:18:52Z")

</div>

but it doesnt fix the problem yet ☹

---

<div class="post-metadata">

**Author:** ![lanx2cool](https://www.phphelp.com/letter_avatar_proxy/v4/letter/l/848f3c/32.png) [@lanx2cool](https://www.phphelp.com/u/lanx2cool)\
**Post date:** [December 17, 2012, 12:25pm UTC](https://www.phphelp.com/t/session-help/19153/15 "2012-12-17T12:25:35Z")

</div>

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](http://www.phphelp.com)

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [December 17, 2012, 4:25pm UTC](https://www.phphelp.com/t/session-help/19153/16 "2012-12-17T16:25:33Z")

</div>

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