0

Salam All Guys... I have stuck in multiple function in php single page

for example i have created a login() and home() when i do my login it shows home but login still there how can i only view one function at a time..

here is the script

  <?php

function login() {
    if (isset($_POST['sub'])) {
        $user   = $_POST['user'];
        $gender = isset($_POST['check']);

        if ($user == "admin" && $gender == "male") {
            echo "oh boy you have access to this site";
            home();
        } else if ($user == "admin" && $gender == "female") {
            echo " Hey Lady.. You have access to this site";
            home();
        } else if ($gender == "" || $gender != "male" || $gender != "female") {
            echo "please select gender";
        } else if ($user == "" || $user == " ") {
            echo "input username";
        } else {
            echo " '" . $user . "' " . " you enter is wrong";
        }
    }
}

?>



<?php
login();
?>  
<form method="POST" >
<input type="text" name="user" placeholder="username" /><br>
Male: <input type="radio" value="male" name="check">  female : <input type="radio"
value="female" name="check" > <br> <input type="submit" name="sub">
</form>

<?php

function home() {
?>

    <p><center>Practice of string a simple test home() </center></p>
 <?php

    $a = "multi thinker ";

    $w    = ucwords($a);
    $trim = trim($a, " ");
    $trim = ltrim($a, " ");
    $trim = rtrim($a, " ");

    echo $w . " " . $trim;

}
?>
2
  • $gender = isset($_POST['check']); is going to set gender to a logical. I don;t think that's what you want since you are checking it later for male, female or empty. Commented Sep 20, 2014 at 18:29
  • @Len_D Thats just a practice script move to the point of functions leave this gender for now :) Commented Sep 20, 2014 at 18:35

2 Answers 2

1
<?php
    function home(){
        header("Location: http://google.com");
    }

    function login() {
        if (isset($_POST['sub'])) {
            $user   = $_POST['user'];
            $gender = isset($_POST['check']);

            if ($user == "admin" && $gender == true) {
                echo "oh boy you have access to this site";
                home();
            } else if ($user == "admin" && $gender == false) {
                echo " Hey Lady.. You have access to this site";
                home();
            } else if ($gender == "" || $gender != "male" || $gender != "female") {
                echo "please select gender";
            } else if ($user == "" || $user == " ") {
                echo "input username";
            } else if ($user != "admin") {
                echo " '" . $user . "' " . " you enter is wrong";
            } else {
            }
        }else{
            //$_POST['sub'] does not exist, therefore output login form
            $out = '
                <form method="POST" >
                    <input type="text" name="user" placeholder="username" /><br>
                    Male: <input type="radio" value="male" name="check">  
                    Female : <input type="radio" value="female" name="check" > <br> 
                    <input type="submit" name="sub">
                </form>';
            echo $out;
        }
    }

login();

?>
Sign up to request clarification or add additional context in comments.

Comments

1

If you are going to put the login and home contents in the same page, you need some state to indicate whether the login succeeded. I see you set $user in your login function. Perhaps make that a global variable and check for its presence like so:

<?php
$user = null; //nothing yet, will be set on successful login

function login() {
    global $user;
    ... //same as you have
}

if( $user === null ) { //have not logged in yet
    login(); //try to log in if we have post data
}
if( $user === null ) { //login failed or was not submitted yet
    ?>

    <form method="POST">
    ... your login form goes here ...
    </form>

    <?php 
    return; //do not show the rest of the home page since we showed login
} //end if $user === null

function home() {
    ... your home function here ...
}
home();
?>

You can improve the arrangement of that, but the basic idea is to have an if-then-else that branches on (a) is the login form being submitted now, and (b) has the user logged in successfully.

It would be better to split this out into multiple files. There are lots of ways to do that, like checking for login and if it is missing issuing a header("Location: http://mydomanin.com/login") redirect, then from the login page redirect back once login has succeeded.

You could also store the $user in a session cookie instead of a global variable so that the user doesn't have to log in on each refresh of the page.

1 Comment

Thanks for guidness.. I understand. home() is not shown when login isnt proceeded.. when login happens home() appears but with home() login() also appears i want only home when login is done and only login when login isnt done.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.