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 10 of 10

Thread: PHP sessions

  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #3
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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??
    }

  • #4
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 { 
    ?>

  • #5
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    what does the full code look like now?

  • #6
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 


    ?>

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    you forgot to move the session_start() like i told you

  • #8
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no I didn't lol. I got it working anyway. I just forgot to "show" that I moved it. Everythings all set. Thanks.

  • #9
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #10
    New Coder
    Join Date
    Feb 2004
    Location
    Salem, Massachusetts. USA
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.)


  •  

    Posting Permissions

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