Thread: Session issue?
View Single Post
Old 01-07-2013, 11:41 AM   PM User | #1
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
Session issue?

Hey everyone,

I seem to be having a session issue and hoping someone could assist me.

I have just finished creating a login form etc an everything seems to be working fine, the only issue i'm having is after the user has logged in, it won't display the username name ie: 'Welcome user'... As per code below, it will only display 'Welcome Guest' so it seems to me that $_SESSION is not working correctly....

This is the code for the verifying details etc
Code:
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="realestate"; // Database name
$tbl_name="registration"; // Table name

// Connect to server and select databse.
$db=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $db)or die("cannot select DB");

// username and password sent from form
global $myusername;
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql= ("SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'];
$_SESSION['mypassword'];
header("location:../login/adproperties success.php");
}
else {
header("refresh: 1; URL=../login/adproperties fail.php");
}

?>
And this is the welcome message code
Code:
<?php
session_start();
if(isset($_SESSION['myusername']))
{
echo 'Welcome , ', $_SESSION['myusername'];
}
else
{
	echo 'Welcome Guest!';
}

?>
I get no error/syntax messages... any idea what could be the issue?

Thanks

Last edited by rexhvn; 01-07-2013 at 11:44 AM..
rexhvn is offline   Reply With Quote