Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP Sessions in FF and Chrome

  • 25-03-2011 3:52pm
    #1
    Registered Users, Registered Users 2 Posts: 378 ✭✭


    Hi guys

    Having Recently made my php site live (thanks to Web Monkey to advice given to my about Sessions) I have encoutered another issue

    I use session_start(); in a header.php file that is called in all my php pages

    In IE, sessions are now working fine, as they should

    In FF and Chrome however, when I login, it creates the session, and loads the relevant page, however when I click on any other php page, the session is lost and I need to log in again

    As I say, its 100% in IE

    Any help would be greatly appreciated and thanks to everyone that has helped me so far in previous threads


Comments

  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    On further investigation

    I have tried IE on another machine and its not working... very very strange and annoying :(


  • Registered Users, Registered Users 2 Posts: 437 ✭✭t1mm


    PHP Sessions are server-side, the browser used shouldn't have any bearing on their working. Try resetting your browsers and making sure you're not running cached versions of the files, and post the code here if you're still stuck!


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    What are you putting in the session? Can you echo the session data to the page after login to see if it looks right? These sort of things are often encoding related.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    Here is the code for the login:

    function checkLogin($u, $p)
    {
    global $seed; // global because $seed is declared in the header.php file

    if (!valid_username($u) || !valid_password($p) || !user_exists($u))
    {
    // the name was not valid, or the password, or the username did not exist
    return false;
    }

    //Now let us look for the user in the database.
    $query = sprintf("
    SELECT loginid
    FROM login
    WHERE
    username = '%s' AND password = '%s'
    AND disabled = 0 AND activated = 1
    LIMIT 1;", mysql_real_escape_string($u), mysql_real_escape_string(sha1($p . $seed)));
    $result = mysql_query($query);
    // If the database returns a 0 as result we know the login information is incorrect.
    // If the database returns a 1 as result we know the login was correct and we proceed.
    // If the database returns a result > 1 there are multple users
    // with the same username and password, so the login will fail.
    if (mysql_num_rows($result) != 1)
    {
    return false;
    } else
    {
    // Login was successfull
    $row = mysql_fetch_array($result);
    // Save the user ID for use later
    $_SESSION = $row;
    // Save the username for use later
    $_SESSION = $u;
    //Save the Firstname for use later
    $_SESSION = $row;
    $_SESSION = $row;
    // Now we show the userbox
    return true;
    }
    return false;

    }

    ?>

    I then use on all my php funcations

    $username = $_SESSION;
    $result = mysql_query("SELECT * FROM profile where username = '$username'");

    With the results then formatted using HTML etc

    So, I can login ok, it works perfectly and loads profile.php page

    If I then click on a hyperlink to any other page, or even to the profile.php again it looses the session information

    Best seen here:

    http://www.myapprentice.ie/

    username: boards
    password: password

    Sometimes sessions work ok in IE

    Best to test in FF or Chrome

    Log in will redirect you to profil.php page with informaiton displayed

    Click on the "profile" link at the top of the page in the banner, and you are logged out and the profile page information is gone!


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    Aren't you supposed to call session_start(); before you can store variables in the $_SESSION[] array? You don't seem to be in the above example, unless I'm mistaking you.

    Are you also making sure that session_start(); is the first line in your file? Even a blank line ahead of it can cause problems.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    Hi there

    I call header.php in every page

    This includes various pages like the db connect settings and also the login functions page which starts the session

    I thought of something this morning? When I call my function, should I pass my variable, for example:

    Show_profile($username);

    Please forgive me as I'm a complete beginner


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    Header.php file:

    <?php
    error_reporting(1); // we don't want to see errors on screen
    // Start a session
    session_start();
    require_once ('db_connect.inc.php'); // include the database connection
    require_once ("functions.inc.php"); // include all the functions
    ?>


  • Registered Users, Registered Users 2 Posts: 437 ✭✭t1mm


    Comment out the error reporting line while you're debugging your code - thats what the errors are there for :)


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    Done and this is what I get:


    Warning: session_start() URL="http://www.myapprentice.ie/function.session-start"][COLOR=#0066cc]function.session-start[/COLOR][/URL: Cannot send session cookie - headers already sent by (output started at D:\Domains\myapprentice.ie\wwwroot\profile.php:12) in D:\Domains\myapprentice.ie\wwwroot\header.php on line 5

    Warning: session_start() URL="http://www.myapprentice.ie/function.session-start"][COLOR=#0066cc]function.session-start[/COLOR][/URL: Cannot send session cache limiter - headers already sent (output started at D:\Domains\myapprentice.ie\wwwroot\profile.php:12) in D:\Domains\myapprentice.ie\wwwroot\header.php on line 5

    Warning: Cannot modify header information - headers already sent by (output started at D:\Domains\myapprentice.ie\wwwroot\profile.php:12) in D:\Domains\myapprentice.ie\wwwroot\re-direct.php on line 11


    Warning: Cannot modify header information - headers already sent by (output started at D:\Domains\myapprentice.ie\wwwroot\profile.php:12) in D:\Domains\myapprentice.ie\wwwroot\login.php on line 12


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Do you call anything or output any html at all before including header.php?

    session_start() needs to be called before php outputs anything else.


  • Advertisement
  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    session_start() has to be called even before your DOCTYPE declaration, so just check again that you're not outputting anything to the browser before calling it.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    All sorted lads

    Thanks a mill for all the help

    I was calling header.php more than once in some parts of the code, I just call it now at the very top of every page and it working fine

    Thanks again

    No doubt I will be on again when I try to get SMTP working :rolleyes:


Advertisement