Web Analytics Made Easy -
StatCounter PHP sessions - CodingForum

Announcement

Collapse
No announcement yet.

PHP sessions

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

  • PHP sessions

    Ok, this login script I've been working on finally works with cookies. But, now I want to make it work with a Session. I've never done a session before. But, I read a tutorial and tried my best to make it work. I have the problem Creating the Session and then seeing if they are logged in.

    PHP Code:
    <?php 
    if ($_POST[user] && $_POST[pass]) { 
    $link mysql_connect ("localhost","username","password"); 
    $database mysql_select_db("database_name"); 
    //$sql = "SELECT * FROM users WHERE username=".$_POST['user']; 
    $sql "select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'"
    $result mysql_query($sql); 

    if (
    mysql_error()) { 
    //  print(mysql_error()); 
    $login_errortrue

    else { 
    // There was no error 
      
    if ($data mysql_fetch_array($result)) { 
        if (
    $data[id] > 0) {   // I HAVE TROUBLE STARTING HERE 
         
    session_start(); 
         
    header("Cache-control: private"); //IE 6 Fix 
         
    $_SESSION['user'] = $_POST['user']; 
         
    mysql_close($link); 
         
    header("Location: test.php"); 
        } 
        else { 
    $login_errortrue; } 
      } 
      else { 
        
    $login_errortrue
      } 
    }   

    }
    //End Of Submit Request 


    if ($login_error== true){ 
    print(
    "Login Error Page. Go Back.<br />"); 

    else { 
    if(
    $_SESSION['user']){ 
    ?> 
    You Are logged in as <?php $_SESSION['user']; ?> 
    <a href=test.php?logout=true><u>Logout</u></a> 
    <?php 

    else { 
    ?> 

    <form action=test.php method=post name="login" style="margin:0;"> 
    Username<br /><input type="text" name="user" size="15" maxlength="20" class="textinput"> 
    <br /> 
    Password<br /><input type="password" name="pass" size="15" maxlength="25" class="textinput"> 
    <br /> 
    <input type="submit" name="submit" value="Submit" class="submit"> 
    </form> 
    <?php 


    ?>

  • #2
    Re: PHP sessions

    if ($data = mysql_fetch_array($result)) {
    if ($data[id] > 0) { // I HAVE TROUBLE STARTING HERE
    session_start();
    header("Cache-control: private"); //IE 6 Fix
    $_SESSION['user'] = $_POST['user'];
    mysql_close($link);
    header("Location: test.php");
    }
    else { $login_error= true; }
    }
    else {
    $login_error= true;
    }
    }
    theres your problem, the function session_start() must be put on the first line of code, before anything gets output to the browser. put it on the first or second line (<?php on first) and it should work fine.

    hope this helps
    php & asp tutorials - the birthplace - biorust - photoshop and web technologies

    Comment


    • #3
      Oh, thanks.

      But, if I wanted to logout...

      <a href=test.php?logout=true><u>Logout</u></a>

      if ($logout == true){
      // ?? how would I make the session disappear??
      }

      Comment


      • #4
        Nevermind. I figured that out. But, It won't display if your logged in.



        PHP Code:
        $name = $_POST[user];

        if($_SESSION['user']){ 
        ?> 
         You Are logged in as <?php echo "$name<br />"?>
         <a href=test.php?logout=true><u>Logout</u></a>
        <?php

        else { 
        ?>

        Comment


        • #5
          what does the full code look like now?

          Comment


          • #6
            It looks like this without the HTML

            PHP Code:
            <?php 
            if ($_POST[user] && $_POST[pass]) { 
            $name $_POST[user];
            $link mysql_connect ("localhost","username","password"); 
            $database mysql_select_db("database_name"); 
            //$sql = "SELECT * FROM users WHERE username=".$_POST['user']; 
            $sql "select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'"
            $result mysql_query($sql); 

            if (
            mysql_error()) { 
            //  print(mysql_error()); 
            $login_errortrue

            else { 
            // There was no error 
              
            if ($data mysql_fetch_array($result)) { 
                if (
            $data[id] > 0) {   // I HAVE TROUBLE STARTING HERE 
                 
            session_start(); 
                 
            header("Cache-control: private"); //IE 6 Fix 
                 
            $_SESSION['user'] = $_POST['user']; 
                 
            mysql_close($link); 
                 
            header("Location: test.php"); 
                } 
                else { 
            $login_errortrue; } 
              } 
              else { 
                
            $login_errortrue
              } 
            }   

            }
            //End Of Submit Request 


            if ($login_error== true){ 
            print(
            "Login Error Page. Go Back.<br />"); 

            else { 
            if(
            $_SESSION['user']){ 
            ?> 
            You Are logged in as <?php echo"$name"?> 
            <a href=test.php?logout=true><u>Logout</u></a> 
            <?php 

            else { 
            ?> 

            <form action=test.php method=post name="login" style="margin:0;"> 
            Username<br /><input type="text" name="user" size="15" maxlength="20" class="textinput"> 
            <br /> 
            Password<br /><input type="password" name="pass" size="15" maxlength="25" class="textinput"> 
            <br /> 
            <input type="submit" name="submit" value="Submit" class="submit"> 
            </form> 
            <?php 


            ?>

            Comment


            • #7
              you forgot to move the session_start() like i told you
              php & asp tutorials - the birthplace - biorust - photoshop and web technologies

              Comment


              • #8
                no I didn't lol. I got it working anyway. I just forgot to "show" that I moved it. Everythings all set. Thanks.

                Comment


                • #9
                  ok, i assumed you didnt because i saw it in the middle, and im pretty sure if php finds it there itll spit out a few errors.

                  btw np
                  php & asp tutorials - the birthplace - biorust - photoshop and web technologies

                  Comment


                  • #10
                    Yeah, I was the lazy moron who just copied the above script. (because I already filled the real one with HTML up the Ying Yang.)

                    Comment

                    Working...
                    X