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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow No session in other pages

    The session is created and stored in the page "login.php" using a login form but when I go to the second page, the session disappears. I have no idea why, really I have tried everything, I show the code, you may have a better view of the code than me (anybody):

    This is the login.php

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php
    session_start();
    
    include_once('overall.php');
    
    if (isset($_POST['mail_log']) && isset($_POST['pass_log'])) {
        //if the user has just tried to log in
        $email=$_POST['mail_log']; 
        $password=$_POST['pass_log']; 
        
        $db_conn=new mysqli('***', '***', '***', '***');
        
        if (mysqli_connect_errno()) {
            echo '<h1>There is no answer from the database</h1>';
            exit;
        }
        
        $query='select * from authorized_users '."where name='$email' "." and password=sha1('$password')";
        
        $result=$db_conn->query($query);
        
        if ($result->num_rows) {
            //if they are in the database register the user id
            $_SESSION['valid_user']=$email;
        }
        $db_conn->close();
    
    }
    
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>...//rest of the page
    In the heading of the login.php is an area created for welcome a member if he is registered in the database or create a form for a member who is not registered.

    I did the test and this works very well, I can create a session with no problems with my email and password stored in the database, but when I go to the second page my session disappears.

    This is the code for the second page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <?php
    session_start();
    
    include('overall.php');
    
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    And in the heading of this second page, the welcome of the member should say "hello!" but shows "login" as no session exists...

    Code:
    <div id="login_area">
    <?php
    			
    create_heading();
    			
    ?>
                </div><!--login_area-->
    Thanks in advanced

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,371
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    You can't have anything before session_start();

    This HAS to be the top of each script ...

    <?php
    session_start();


    So change it up ...

    <?php
    session_start();
    echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";


    like that instead.


    .

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    above EVERYTHING ??? I will test this ... thanks in advanced !

  • #4
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much, I really appreciate your help, sincerely I was at least like 5 hours trying to solve it, however I will never forget how to do it.... in my entire life....

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by renzocj View Post
    above EVERYTHING ??? I will test this ... thanks in advanced !
    From PHP.net:
    Note:

    To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
    For more information about this, see the headers already sent link in my signature.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you tangoforce


  •  

    Tags for this Thread

    Posting Permissions

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