dont know how to use session in login function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bb nicole
    New Member
    • Jan 2007
    • 127

    #1

    dont know how to use session in login function

    I want 2 use session in my login, but i never use session before. How to change my code as below if i want to use session... Can somebody give me some opinion, please.. Thanks a lot.. Below is my php code..

    LOGIN FUNCTION
    [PHP]<?php
    extract ($_POST);

    mysql_connect(" localhost","roo t","")
    or die("Cannot connect to the server");

    mysql_select_db ("ums e-job portal")
    or die("Cannot connect to the database");

    $sql="select username from company where username='$user name'";

    $result=mysql_q uery($sql)
    or die("Cannot execute query");

    $num=mysql_num_ rows($result);

    if($num==1) //username valid
    {
    $sql="select username from company where username='$user name' and password='$pass word'";

    $result2=mysql_ query($sql)
    or die("Cannot execute query");

    $num2=mysql_num _rows($result2) ;
    if($num2>0)
    {
    setcookie("User name", $username, time() + 60 * 60 * 24 * 365);
    $today= date("D F d, h:ia");

    include("welcom e.php");

    }
    else //message send to emp_login.php when wrong password
    {
    $message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
    include("emp_lo gin.php");
    }
    }
    elseif($num==0) //message send to emp_login.php when username not valid
    {
    $message="The username you entered does not exist! Please try again.<br>";
    include("emp_lo gin.php");
    }
    ?>[/PHP]

    WELCOME PAGE
    [PHP]<html>
    <head>
    <title>Welcom e</title>
    </head>
    <body>
    <h1 align="center"> <?php echo $username; ?>, welcome to UMS e-Job Portal!</h1>
    <h3 align="center"> You have been logged in since: <?php echo $today; ?>. </h3>
    <?php
    include("homepa ge.php");
    ?>
    </body>
    </html>[/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    When you want to use the $_SESSION array, you can do the following:
    start each script with command[php]start_session() ;[/php].
    In your login script you can then save, say, the username, e.g.[php]$_SESSION['username'] = $username;[/php].
    This variable is available to each script you use after it and has the session_start() ; at its beginning. You can extract the username using [php]$myuser = $_SESSION['username'];[/php]
    Ronald :cool:

    Comment

    • foekall
      New Member
      • Jan 2007
      • 28

      #3
      Thz bro.........

      Comment

      • sandeepvanarase
        New Member
        • Jan 2007
        • 1

        #4
        That's not possible

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Would be nice to our members if you would explain what exactly is not possible!

          Ronald :cool:

          Comment

          • foekall
            New Member
            • Jan 2007
            • 28

            #6
            I already write 3 fles
            (1) login.php
            (2) login_success.p hp
            (3) logout.php
            [php]
            (1) Login.php code is here
            <?php
            ob_start();
            include "../db_connection.p hp";
            if(isset($_POST['submit']))
            {
            $email=($_POST['txtemail']);
            $password=($_PO ST['txtpass']);
            $confirm="";
            $login="SELECT * FROM table WHERE Email='$email' && Password='$pass word' '";
            $result=mysql_q uery($login);
            $count=mysql_nu m_rows($result) ;
            if($count==1)
            {
            session_registe r("txtemail") ;
            session_registe r("txtpass");
            header('Locatio n: login_success.p hp');
            }
            else
            {
            echo "Error";
            }
            }
            ob_end_flush();
            ?>
            <html>
            <body>
            <table cellspacing="0" cellpadding="0" border="0">
            <form name="frmlogin" method="POST" action="login.p hp">
            <input type="text" name="txtemail" ><br>
            <input type="password" name="txtpass"> <br>
            <input type="submit" name="submit" value="Login">
            </form>
            </table>
            </body>
            </html>

            (2) login_success code is
            <?php
            session_start() ;
            if(!session_is_ registered('txt email'))
            {
            header("locatio n: login.php");
            }
            ?>
            In this login_success page I want to get Login name from login.php page how to I get..........


            (3) logout.php code is
            <?
            session_start() ;
            session_destroy ();

            echo "Successful ly logout";
            ?>[/php]
            Last edited by ronverdonk; Jan 31 '07, 04:53 PM. Reason: enclosed code in tags

            Comment

            • bb nicole
              New Member
              • Jan 2007
              • 127

              #7
              Thanks, Ronald.. :) I will try it first, if got any problem, i will post to this forum again.. Thanks a lot..

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by foekall
                I already write 3 fles
                (1) login.php
                (2) login_success.p hp
                (3) logout.php

                (1) Login.php code is here
                <?php
                ob_start();
                include "../db_connection.p hp";

                if(isset($_POST['submit']))
                {
                $email=($_POST['txtemail']);
                $password=($_PO ST['txtpass']);
                $confirm="";
                $login="SELECT * FROM table WHERE Email='$email' && Password='$pass word' '";
                $result=mysql_q uery($login);
                $count=mysql_nu m_rows($result) ;
                if($count==1)
                {
                session_registe r("txtemail") ;
                session_registe r("txtpass");
                header('Locatio n: login_success.p hp');
                }
                else
                {
                echo "Error";
                }
                }
                ob_end_flush();
                ?>
                <html>
                <body>
                <table cellspacing="0" cellpadding="0" border="0">
                <form name="frmlogin" method="POST" action="login.p hp">
                <input type="text" name="txtemail" ><br>
                <input type="password" name="txtpass"> <br>
                <input type="submit" name="submit" value="Login">
                </form>
                </table>
                </body>
                </html>
                (2) login_success code is
                <?php
                session_start() ;

                if(!session_is_ registered('txt email'))
                {
                header("locatio n: login.php");
                }

                ?>
                In this login_success page I want to get Login name from login.php page how to I get..........


                (3) logout.php code is
                <?
                session_start() ;
                session_destroy ();
                echo "Successful ly logout";
                ?>
                Where is your session_start() in the login.php?
                Next time, when you display code, enclose it within code or php tags!!

                Ronald :cool:

                Comment

                • foekall
                  New Member
                  • Jan 2007
                  • 28

                  #9
                  Originally posted by ronverdonk
                  Where is your session_start() in the login.php?
                  Next time, when you display code, enclose it within code or php tags!!

                  Ronald :cool:
                  i use session_start() ; and session_destroy (); in logout.php.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by PHP docs
                    If you want your script to work regardless of register_global s, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_registe r(), it will not work in environments where the PHP directive register_global s is disabled.

                    register_global s: important note: Since PHP 4.2.0, the default value for the PHP directive register_global s is off, and it is completely removed as of PHP 6.0.0. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.
                    See the above warning from the PHP documentation. So, as I said before, it is better to use the $_SESSION array fro passing variables. That makes your code e.g.

                    login.php:[php]session_start() ;
                    // at the spot where you do the session_registe r do this instead:
                    $_SESSION['txtemail'] = $email;
                    $_SESSION['txtpass'] = $password;
                    [/php]
                    login_success.p hp:[php]<?php
                    session_start() ;
                    if(!isset($_SES SION['txtemail'))
                    header("locatio n: login.php");
                    else
                    echo "Welcome, ".$_SESSION['txtemail'];
                    ?>[/php]
                    logout.php code:[php]<?php
                    session_start() ;
                    setcookie(sessi on_name() ,"",0,"/");
                    unset($_COOKIE[session_name()]);
                    $_SESSION = array();
                    session_unset() ;
                    session_destroy ();

                    echo "Successful ly logged out";
                    ?>[/php]
                    Ronald :cool:

                    Comment

                    Working...