SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_SESSION won't work

    It works when i test the script in my local webserver, but when i upload it to my account at a webhosting company it wont work !!

    I even tried to echo the session variable in the other script but it didn't show , so it seems like it wasn't registered as a session variable at all.

    here's the script that checks on login and forwards to control panel :
    PHP Code:
    <?php
        
    if($_POST['user'] == 'myuser' && $_POST['pass'] == 'mypass')
        {
           
    session_start();
             
    $_SESSION['logged_in_user'] = 'myuser';
             
    header('Location: http://www.mysite.com/cp.php');
        }
        else
           echo 
    '<b>Wrong user\password , hit back in your browser to re-enter info</b>';
             

    ?>
    and the beginning of cp.php there's :
    PHP Code:
    <?php session_start(); ?>
    <?php 
    if ($_SESSION['logged_in_user'] == 'myuser') : ?>
    /* display control panel */
    <? else : ?>
    <b>You are not allowed to access this page.</b>

    <? endif;  ?>
    i always get the "you are not allowed..." message.


    any help is MUCH appreciated

    [/php]

  2. #2
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What version of PHP are you using? Because $_SESSION wasn't added until PHP 4.1.0.

  3. #3
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    version 4.3.10

  4. #4
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is register globals enabled? I had them interfere with a script that used sessions not so long ago. Also is your server set up right for sessions (are they enabled in php.ini and does the directory they're supposed to be saved in exist)?

  5. #5
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Maine
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, don't use the if: endif; style. Use curly brackets.


  6. #6
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *EDIT* problems solved
    Last edited by mPeror; Mar 30, 2005 at 16:22.

  7. #7
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mPeror
    TomH :i don't really not , its not my server.
    Upload a page that contains the phpinfo() function and check. It would have something to do with it.

    Regarding your errors - they're probably appearing as output is being sent to the browser before your call to session_start(). This has to come before any echo statements or breaks for HTML code (including white space).

  8. #8
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks tom , i just figured this out

  9. #9
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem.

  10. #10
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How did you fix it

    Could you please tell me how you got it solved I am having the same problem and i tried everything suggested here and still it wouldnt work

    Some help will be great

    Thank you

  11. #11
    SitePoint Guru arunkumar's Avatar
    Join Date
    Jan 2005
    Location
    India:Chennai
    Posts
    827
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    What is your problem is with session???

    then check by phpinfo whether session is enabled or not..
    Where there is a will there is a way
    ArunKumar

  12. #12
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I checked phpinfo my sessions
    session are set to followings
    Session Support enabled
    Registered save handlers files user

    when I try to authenticate a user sometimes it works and sometimes it doesnt Below is my code. Please send your feed back

    adminlogin.php
    <?
    session_start();
    $user = 'test';
    $pass = 'test';
    if (isset($_POST['login']))
    {
    if ($_POST['user'] == $user && $_POST['pass'] == $pass)
    {
    $_SESSION['login'] = true;
    // session_register("user");
    // echo 'Login SUCCESSFULL';
    }
    else
    {
    echo 'Login unseucessful'; }
    }
    if (isset($_GET['action']) && $_GET['action'] == 'logout' &&isset($_SESSION['login']))
    {
    unset($_SESSION['login']);
    session_destroy();
    echo 'user logged out';
    }
    if (isset($_SESSION['login']) && $_SESSION['login'] == true)
    {
    header("Location: adminTest.php"); //24
    }
    else
    {?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Username<br /> <input type="text" name="user"><br />
    Password<br /> <input type= "password" name="pass"><br />
    <input type="submit" name="login" value="Login">
    </form>
    <?php
    }



    Thank you
    ?>

    //admintest.php
    <?php
    session_start();
    $log = $_SESSION['login'];
    //echo " Session login set to $log <br>";
    //$_SESSION['count'] =
    if ($_SESSION['login'] == true)
    {
    echo '<tr><td>U are in test main</td></tr>';
    $login = $_SESSION['login'];
    echo "login is $login <br>";
    }
    else
    {
    echo '<tr><td>U are not logged in TestMain</td></tr> <br>'; $login = $_SESSION['login'];
    echo "login is $login <br>";
    }
    ?>

    //admintest2.php
    <?php
    session_start();
    if (isset($_SESSION['login']) && $_SESSION['login'] == true)
    {
    echo '<tr><td>U are in Test2</td></tr>';
    }
    else
    {
    echo '<tr><td>U are not logged in</td></tr>';

    }


    ?>

    //logout.php
    <?
    session_start();
    session_destroy();

    //echo "u are logged out now <br>";
    //redirect("adminLogin.php");
    header("Location: adminLogin.php");

    ?>


Bookmarks

Posting Permissions

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