Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Help needed to make a login script redirect to another reserved page

$
0
0

Please, help. I'm new to php but I've tried so many times to make it work.

I've wrote a script that reads user and password data correctly, to log into a reserved webpage. The problem is that when you press the login button, it doesn't redirect to the target.php (reserved) page. The form is in a file called login.php. It reads and connect to the db, but stays on this login page. As a matter of fact, if I manually type in the url mywebsite/target.php after putting the credentials, it works. But I need an automatic redirect from login.php page and if credentials are correct, redirect to target.php.

<?php


    session_start();


    function loginform(){

        echo "<form action='' method='POST'>
              Username: <input type = 'text' name='username'>
              Password: <input type = 'text' name='password'>
              <input type = 'submit' name='login' value='Login'>
              </form>

        ";
    }

    function logoutform(){

        echo "<form action='' method='POST'>
              <input type = 'submit' name='logout' value='Logout'>
              </form>

        ";

    }

    function login($username, $password){

        $pass = md5($password);

        $con= mysqli(whateverwebsite.com, 'Nameofthetable', 'hashedmd5password') or die (mysql_error());

        mysql_select_db('whateverdb', $con) or die (mysql_error());

        $result = mysql_query(" SELECT * FROM user WHERE username='$username' AND password='$pass' ") or die (mysql_error);

        $count= mysql_num_rows($result);

        if($count==1) {
            $_SESSION['login']=$username;
            header('Location:Aggiornamenti/Aggiornamenti.php'); /*this doesn't actually work*/
        }

        else {
            header('Location:index.php'); /*this doesn't actually work*/
            echo "Wrong login"; 
        }

    }

    function logout(){
        session_destroy();
    }

    if (isset($_SESSION['login'])) {       /*this function seems to be ignored*/
        echo "Success";
        logoutform();
    }

    else{
        echo "Log in with username and password.";
        loginform();
    }

    if ($_POST['login']) {
        login($_POST['username'], $_POST['password']);
    }

    elseif($_POST['logout']){
        echo "you are logging out";
        logout();
    }

?> 

also, Before the html of the target.php page, there is this

<?php

session_start();
echo "Reserved area<br>";

if (!isset($_SESSION['login'])) {
exit("you must login <a href='../login.php'>Login<a>");
}
else {
echo "Do the <a href='../login.php'>Logout</a>";
}

?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles