www.webdeveloper.com
Results 1 to 7 of 7

Thread: two Problems with SESSION

  1. #1
    Join Date
    Mar 2012
    Posts
    11

    two Problems with SESSION

    Friends
    I got a problem with sessions desribed as following

    when i established a login form and verified username and password obtained by this form with these stored into Data base using php code then if both username and password are ok comparing to these saved into database i establish a session for the user but this last step dosen,t work and no session established although it was working in my last website i emlemented but not working with this website i,m doing right now i tried much with the code but it did not work
    here is the code i,m using
    PHP Code:
    {   
            
    /*getting login variables */
            
    $username strip_tags(@$_POST['username']);
            
    $userpass strip_tags(@$_POST['password']);
                
            
    $submit =@$_POST['usersubmit']; (when user press submit button )
            if (
    $submit){
             
    /* Data base connection */
            
    include ("config.php");
            
    /* check for username and password*/
            
    if (!$username==""){
            if (!
    $userpass==""){
    //convert obtained pass into MD5 to compare it with the stored one 
            
    $userpass md5(strip_tags(@$_POST['password']));
            
            
    $query mysql_query("SELECT * from commercial_members WHERE username='$username'") or die("couldn,t connect");
            
    $num mysql_num_rows($query);
            if (
    $num!=0){
            while(
    $row mysql_fetch_assoc($query)){
            
    $dbusername=$row['username'];
            
    $dbpassword=$row['userpass'];
        }
            if (
    $userpass==$dbpassword){

    //this( yes yes yes )is for test and i can see it printed when username and password are correct
    echo" yes yes yes ";
    // this is the session and it was and still working in my previous sites i emplemented before but not with this site and it was written the same way you can see 
         
    @$_SESSION['username']=$username;

    // i tried it also this way but it did not work 
     
    $username=@$_SESSION['username'];


    }else {echo 
    " <font color='red'><b>Sorry wrong password   </b></font>";}
    }else {echo 
    " <font color='red'><b>this user name is not registered  </b></font>";}
    }else {echo 
    "  <font color='red'><b> you did not enter password </b></font>";}
    }else {echo 
    "  <font color='red'><b> you did not Enter user name </b></font>";}
        }

    that was the code and all i can see when the username and password are correct is this ( yes yes yes ) that i ordered to echo it just for test
    i,m sure this problem is related with php.ini and if there was something wrong with this file ( suppose it was modified by a hacker for example or by any mistake how can i correct it


    i also have anothe problem with session is that when i loging into any of my previous sites with a username and a password and establish a session and see a username printed ( welcome - username !) when i move to another page i don,t see this session although the session is started in this page and all pages that need to start session and this problem happened to the last website i had

    waiting for your replies
    Thank you
    Last edited by NogDog; 04-15-2014 at 03:10 PM. Reason: changed [color] tags to [php] tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Do you have a session_start() somewhere before (a) you reference $_SESSION in any way and (b) generate any output (which sends HTTP headers)?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    First time I've seen that syntax but why are you placing a @ before $_POST and $_SESSION variables?

    Assuming the session variables contain information and are not null, then you probably are missing a session_start() at the top of your page.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    "@" suppresses many warnings/notices. IMO, it's a lazy way to avoid doing a little defensive coding, while making it more difficult to debug.

    You can save yourself maintenance grief with:
    PHP Code:
    $username null;
    if(isset(
    $_SESSION['username'])) {
        
    $username $_SESSION;

    That's probably the easiest to read/maintain, but if you just abhor typing multiple lines in spite of it being easy to quickly read and debug, then there's always the ternary operator:
    PHP Code:
    $username = isset($_SESSION['username']) ? $_SESSION['username'] : null
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2012
    Posts
    11
    when i see the web site from my local host ( from laptop ) it goes ok but when i see it from cpanel ( online ) i see this message

    (
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/ansaferc/public_html/index.php:3) in /home/ansaferc/public_html/header1.php on line 4

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ansaferc/public_html/index.php:3) in /home/ansaferc/public_html/header1.php on line 4
    )

  6. #6
    Join Date
    Apr 2014
    Posts
    1
    You seem to have some white space (spaces, tabs, line breaks, ...) at the beginning of the file.
    Please delete all the characters before <?PHP and add ob_start(); before session_start(); to be safe.
    If you use UTF-8, be sure to save the file without BOM.

  7. #7
    Join Date
    Mar 2012
    Posts
    11
    as i said b4 when i run this website from pc (local host ) it is ok and i can see the session in all pages
    but when i run it online (www.the domain name.com ) i see the session only in the main page when i sign in then if i move to any other page i get these message i mentioned the web pages are uploaded into the public_html and i uploaded some pages at the (/home/ ) could this be the reason ?
    this is my first time i upload web page using cpanel and there may be something wrong i did dand i don,t know i,m not an expert with cpanel

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles