I'm using jQuery for mobile and making the app show users a dailog box to let them enter their credentials when they want to log into the app. Ok. it redirects them to the home page but the url stays as mysite.com/login.php rather than mysite.com/index.php This removes the menu bar on the bottom but it appears if the page is refreshed which I don't want my users to do. Since I'm using a dialog box for the log in page, how would I display any errors if username/password isn't correct? If I do though, they will be redirected to the actual login.php page and dialog box disappears.

Why is that ? any help will be appreciated. Thank you.

 <?php
include('funcs/connector.php');
$port = new MySqlDatabase();
$port ->connect('root','','myDB') or die(mysql_error());
session_start();


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

$query = "SELECT username, password FROM USERS WHERE `username` = '$user' AND `password`='$pass' ";
$result = mysql_query($query) or mysql_error();

$row = mysql_fetch_assoc($result);

if( $username == $row['username']){
    $_SESSION['username'] = $row['username'];
    header('Location:index.php');
}else{
    echo "this is wrong";
}

Recommended Answers

All 4 Replies

Member Avatar for diafol

So is the login box supposed to appear automatically when the site is entered?

I'd use a session variable to keep track of whether the user was logged in. If not, the login screen (no menu) is shown, otherwise, the full menu is displayed:

It may help if all pages redirect to a login page or call a login box if no session data is found. This code can be placed into an include file and included at the top of all pages.

session_start();
if(isset($_SESSION['user_id'])){
    ....
}else{
    ....
}

Yeah, on the home page, I have got a session variable that checks whether users are logged in or not. What I am facing though is that once they go to the login.php and log in successfully. Then they are redirected to the home page and can view the content of it, but the url doesn't change to home.php and unfortunately stays as log_in.php which is the script being written in the first post. This is what I have got in the login.php page. Nah, I didn't do any checking for the session variables on it yet. It is wierd to have the URL changed when users log in or out. I never had this problem ever. Do you think jQuery for mobile is causing it?

<?php 

session_start();

?>
<!DOCTYPE html>

<html lang="en">

<head>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>    

</head>



<body>



<div data-role="header"> 
    <h1>Login</h1>
</div>


<div>


</div>

    <form action="log_in.php"  method="POST" >

            <label for="username"> <input type="text" name="username" id="username" placeholder="Username"> </label>
            <label for="password"> <input type="password" name="password" id="password" placeholder="Password"> </label>

    <input type="submit" value="log in">



    </form>




</body>




</html>
Member Avatar for diafol

Nowt wrong with the form AFAIK.
As for the code;

$row = mysql_fetch_assoc($result);
if( $username == $row['username']){
    $_SESSION['username'] = $row['username'];
    header('Location:index.php');
}else{
    echo "this is wrong";
}

looks a bit odd, how's this:

if(mysql_num_rows($row)){
    header('Location: index.php');
}else{
    echo 'wrong somehow';
}

That works exactly the same as the if-statement that I had but it still causes the same error which is not changing the URL to index.php and users can still view the content of it.. I just took out the jQuery files out and it seems to work fine without them. I guess the problem is caused by jquery mobile or jquery..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.