Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Session issue?

  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    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");
    }

    ?>

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    66
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Works great, thank you so much.

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If we want to add the specific validation like the people only from particular areas, then which code will we use?

  • #5
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    People from specific areas? You mean like Geo-targeting?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •