Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page refresh after login?

I am trying to get my page to refresh after login this is the format of my index.php

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
    // let the user access the main page  
    // i have made the main content with jquery mobile framework 
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
    // let the user login
       include "login.php";

}
else
{
    // display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
            <fieldset>
                <label for="username">Username:</label><input type="text" name="username" id="username" /><br />
                <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
                <input type="submit" name="login" id="login" value="Login" />
            </fieldset>
            </form>

Inside my login.php

<?php

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

    if(mysql_num_rows($checklogin) == 1)
    {
        $row = mysql_fetch_array($checklogin);
        $email = $row['email'];
        $uid = $row['u_id'];

        $_SESSION['Username'] = $username;
        $_SESSION['EmailAddress'] = $email;
        $_SESSION['LoggedIn'] = 1;
        $_SESSION['uid'] = $uid;
        $flag=1;

    header("Location: http://5aday.dihops.net/index.php");
//        echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">";
//header("Refresh: 2; url=http://5aday.dihops.net/index.php");
    // THIS IS WHERE I AM STUCK 
      }
    else
    {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
    }
?>
    }
    ?>

I dont know what I am doing wrong. But once the user gives his username and password and submits it .... I get a blank page. and the user has to manually refresh the webpage to see the content of the webpage.

like image 548
KMC Avatar asked Jun 25 '26 19:06

KMC


1 Answers

As per the link mentioned the header(location) must be placed at the top of the page ... So I rearranged the if conditions ....

I added this at the top of the index.php

if(!empty($_POST['username']) && !empty($_POST['password']))
{
   include "login.php";


} ?>

then in the body i added the other if statements

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
    // let the user access the main page  
    // i have made the main content with jquery mobile framework 
}else 
else
{
    // display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
            <fieldset>
                <label for="username">Username:</label><input type="text" name="username" id="username" /><br />
                <label for="password">Password:</label><input type="password" name="password" id="password" /><br />
                <input type="submit" name="login" id="login" value="Login" />
            </fieldset>
            </form>
}

That has fixed the problem.

like image 88
KMC Avatar answered Jun 27 '26 07:06

KMC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!