Web Analytics Made Easy -
StatCounter Session Problems - CodingForum

Announcement

Collapse
No announcement yet.

Session Problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session Problems

    I am having some trouble with sessions. I am creating a user login system and everything works apart from the sessions it appears as though they are not being created. I dont know if it is my code or server. I am running a local testing server which uses easy php if you have to enable sessions in the php.ini file then that is why it is not working. If you have to do this how do I?

    The only other thing it could be is my code:
    login.php
    PHP Code:
    <?php
    session_start
    ();
    require_once (
    'dbconnect.php');
    if (isset(
    $_POST['username']) && isset($_POST['password'])) {
        
    $user stripslashes($_POST['username']);
        
    $pass stripslashes($_POST['password']);
        
    $query mysql_query("SELECT * FROM members WHERE username='$user' AND password='$pass'") or die (mysql_error());
        if (
    mysql_num_rows($query) > 0)
        {
            
    $_SESSION['username'] = $user;
            
    $_SESSION['loggedin'] = true;
            
    header ("Location: main.php");
        }else{
            print 
    "<p><ul><li>thake.inertnal: Invalid login, please try again.</li></ul></p>\n";
        }
    }
    ?>
    <?php 
    if (isset($_GET['action']) && $_GET['action'] == 'loginform') {?>
    <form action="login.php" method="post">
        <table style="text-align:center">
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Login" /></td>
            </tr>
        </table>
    </form>
    <?php ?>
    main.php
    PHP Code:
    <?php
    session_start
    ();
    if (!isset(
    $_SESSION['loggedin']) || !$_SESSION['loggedin'])
    {
        print 
    "<p><ul><li>You are not logged in.</li></ul></p>\n";
    }else{
        print 
    "<p><ul><li>You are logged in.</li></ul></p>\n";
    ?>
    Anyone see anything wrong/ know how to enable sessions in the php.ini file.

  • #2
    It must be something about how you installed PHP because your code works on my site. I don't know anything about easy PHP... normal PHP includes session support by default.

    Comment


    • #3
      The folder where php saves the session data to must exist. I suspect that you are using windows?

      In the php.ini there is a line - session.save_path = "/stemp"

      This must be set to a folder that exists (stemp is the folder name use in the root of my hard disk on my test server.)

      If you put the following line in after your opening <?php tag, it will probably provide some information as to why sessions are not working -
      PHP Code:
      error_reporting(E_ALL); 
      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

      Comment


      • #4
        Its strange because I have changed the directory to one in the root of the server and the session files are being written to it.
        When I tried to login it created three session files one of which read:
        Code:
        username|s:9:"admintest";loggedin|b:1;
        I am running easyphp on a windows 2003 server on a LAN and have tried accessing the pages from the server and other machines on the newtwork. The only thing I can think of is that there is something wrong with the if statement in main.php but I can't see anything wrong with it.

        Also the error reporting code you suggested doesn't throw up a thing.

        Comment


        • #5
          Because the session file was created, sessions are enabled on your server.

          Because this code worked for Fumigator, about the only thing would be if the session cookie is not being set/accepted. When you browse to the main page, there is no cookie to tie the session_start to an existing session.
          Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

          Comment


          • #6
            There appears to be a problem with my config file somewhere with cookies. I have installed easy php on another machine tried it and everything works.

            The cookie is not being sent by my server, which config file are cookies controlled from the php one or the apache one??
            The other thing it could be is that there is a security setting on Windows Server 2003 which makes it so cookies can not be sent out, anyone know if this is tru and how to change it so cookies can be sent?

            Thanks.
            Last edited by manicman; Oct 10, 2006, 04:17 PM.

            Comment

            Working...
            X