D.Saha February 2016

i am trying to practice a simple session code in my house in xamp

but some how the page is not showing any error message. also it is not redirecting to the page where i want to redirect it after successful login. i want the login page to be redirected at home page and show the message and when the logout link is clicked the page again come back to the login page

<?php    
    session_start();
    error_reporting(E_ALL ^ E_NOTICE);
    ?>
    <?php
        if($_REQUEST["logout"]=='yes'){
        unset($_SESSION["login"]);
        }
    ?>
    <html>
        <body>
            <form action="" method="post">
                <label>Username</label><input type="text" name="unametxt" value="<?php echo $_post["unametxt"]; ?>" />
                <label>Password</label><input type="password" value="<?php echo $_post["password"]; ?>" />
                <input type="submit" name="sbt" value="Login">
            </form>

            <?php
                if(isset($_post["sbt"]))
                {
                    if($_post["unametxt"]== "debarun" && $_post["password"]=="1234")
                    {
                        $_SESSION["login"]="yes";
                        $_SESSION["uname"]=$_post["unametxt"];
                        $_SESSION["passwd"]=$_post["password"];

                        header('location:home.php');

                        }
                    else
                    {
                        echo "Please enter correct credentials";
                        }   
                    }
             ?>
        </body>
    </html>



    and it is my home page script:

    <?php
    session_start();

    if(!isset($_SESSION["login"])){
        session_destroy();
        header('location:login.php');

    }

    else{
        echo "Welcome".$_SESSION["uname"]."<br/>"."your password is".$_SESSION["passwd"];
        }

    ?>

<html>
    <body>
        <fo        

Answers


Sirjan Sharma February 2016

Put all those validation code at the top. Nothing should be sent to the browser before redirecting. Not even an empty line.

Also, make the L in location capital

    header("Location: home.php");


Edwin Alex February 2016

Add name attribute in your password element. Because of this, it cannot fetch from $_POST array and your condition will always fail.

Try this,

<label>Password</label><input type="password" name="password" value="<?php echo $_post["password"]; ?>" />


Mulham Aryan February 2016

First thing $_post must be up letter case like this $_POST then you forgot to specify name="password" to password input field take a look

<?php    
    session_start();
    error_reporting(E_ALL ^ E_NOTICE);
    ?>
    <?php
        if($_REQUEST["logout"]=='yes'){
        unset($_SESSION["login"]);
        }
    ?>
    <html>
        <body>
            <form action="" method="post">
                <label>Username</label><input type="text" name="unametxt" value="<?php echo $_POST["unametxt"]; ?>" />
                <label>Password</label><input type="password" name="password" value="<?php echo $_POST["password"]; ?>" />
                <input type="submit" name="sbt" value="Login">
            </form>
            <?php
                if(isset($_POST["sbt"]))
                {
                    echo $_POST["password"];
                    if($_POST["unametxt"] == "debarun" and $_POST["password"] == "1234")
                    {
                        $_SESSION["login"]="yes";
                        $_SESSION["uname"]=$_POST["unametxt"];
                        $_SESSION["passwd"]=$_POST["password"];

                        header('location:home.php');

                        }
                    else
                    {
                        echo "Please enter correct credentials";
                        }   
                    }
             ?>
        </body>
    </html>

Post Status

Asked in February 2016
Viewed 2,377 times
Voted 7
Answered 3 times

Search




Leave an answer