Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 01-07-2013, 12:52 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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");
}

?>
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 01-08-2013, 10:06 PM   PM User | #3
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
Works great, thank you so much.
rexhvn is offline   Reply With Quote
Old 01-11-2013, 11:08 AM   PM User | #4
paulinetaylor85
New Coder

 
Join Date: Oct 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
paulinetaylor85 is an unknown quantity at this point
If we want to add the specific validation like the people only from particular areas, then which code will we use?
paulinetaylor85 is offline   Reply With Quote
Old 01-11-2013, 07:17 PM   PM User | #5
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
People from specific areas? You mean like Geo-targeting?
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:34 AM.


Advertisement
Log in to turn off these ads.