CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Session issue? (http://www.codingforums.com/showthread.php?t=285396)

rexhvn 01-07-2013 11:41 AM

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

Redcoder 01-07-2013 12:52 PM

Have a session_start() at the top of the login page too - and in general every page where you'll ever need to get, manipulate or destroy session variables - and asign $username to $)SESSION['myusername'].

PHP Code:

<?php

session_start
();

$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'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
header("location:../login/adproperties success.php");
}
else {
header("refresh: 1; URL=../login/adproperties fail.php");
}

?>


rexhvn 01-08-2013 10:06 PM

Works great, thank you so much.

paulinetaylor85 01-11-2013 11:08 AM

If we want to add the specific validation like the people only from particular areas, then which code will we use?

Redcoder 01-11-2013 07:17 PM

People from specific areas? You mean like Geo-targeting?


All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.