How to redirect to another page using PHP

前端 未结 12 1370
粉色の甜心
粉色の甜心 2020-12-02 16:50

I am building a website which includes a login page. I need to redirect the user to their profile page once they\'ve logged in successfully, but I don\'t know how to do that

相关标签:
12条回答
  • 2020-12-02 17:35

    That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself). So how do I redirect the user from one page to the next?

    This means your application design is pretty broken. You shouldn't be doing output while your business logic is running. Go an use a template engine (like Smarty) or quickfix it by using output buffering).

    Another option (not a good one though!) would be outputting JavaScript to redirect:

    <script type="text/javascript">location.href = 'newurl';</script>
    
    0 讨论(0)
  • 2020-12-02 17:36

    header won't work for all

    Use below simple code

    <?php
            echo "<script> location.href='new_url'; </script>";
            exit;
    ?>
    
    0 讨论(0)
  • 2020-12-02 17:44

    The simplest approach is that your script validates the form-posted login data "on top" of the script before any output.

    If the login is valid you'll redirect using the "header" function.

    Even if you use "ob_start()" it sometimes happens that you miss a single whitespace which results in output. But you will see a statement in your error logs then.

    <?php
    ob_start();
    if (FORMPOST) {
        if (POSTED_DATA_VALID) {
            header("Location: https://www.yoursite.com/profile/");
            ob_end_flush();
            exit;
        }
    }
    /** YOUR LOGINBOX OUTPUT, ERROR MESSAGES ... **/
    ob_end_flush();
    ?>
    
    0 讨论(0)
  • 2020-12-02 17:44
    firstly create index.php page and just copy paste below code :-
    
    <form name="frmUser" class="well login-form" id="form" method="post" action="login_check.php" onSubmit="return FormValidation()">
        <legend>
            <icon class="icon-circles"></icon>Restricted Area<icon class="icon-circles-reverse"></icon>
        </legend>
        <div class="control-group">
            <label class="control-label" for="inputPassword">Username</label>
            <div class="controls">
                <div class="input-prepend">
                    <span class="add-on"><icon class="icon-user icon-cream"></icon> </span>
                    <input class="input" type="text" name="username" id="username" placeholder="Username" />
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPassword">Password</label>
            <div class="controls">
                <div class="input-prepend">
                    <span class="add-on"><icon class="icon-password icon-cream"></icon>
                    </span> <input class="input" type="password" name="password" id="password" value="" placeholder="Password" />
                </div>
            </div>
        </div>
        <div class="control-group signin">
            <div class="controls ">
                <input type="submit" class="btn btn-block" value="Submit" />
                <div class="clearfix">
                    <span class="icon-forgot"></span><a href="#">forgot password</a>
                </div>
            </div>
        </div>
    </form>
    
    
    
    /*------------------after that ----------------------*/
    
    create a login_check.php and just copy paste this below code :-
    
    <?php
    session_start();
    include('conn.php');
    
    <?php
    /* Redirect browser */
    header("location:index.php");
    
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>
    
    
    <?php
    
    if(count($_POST)>0)
    {   
    
        $result = mysql_query("SELECT * FROM admin WHERE username='".$_POST["username"]."' and password = '".$_POST["password"]."'");
        $row  = mysql_fetch_array($result);
    
    if(is_array($row)) 
    {
        $_SESSION["user_id"] = $row[user_id];
        $_SESSION["username"] = $row[username];
    
        $session_register["user_id"] = $row[user_id];
        $session_register["username"] = $row[username];
    } 
    else 
    {
       $_SESSION['msg']="Invalid Username or Password";
       header("location:index.php");
    }
    }
    
    if(isset($_SESSION["user_id"]))
    {
        header("Location:dashboard.php");
    }
    
    ?>
    
    
    
    
    /*-----------------------after that ----------------------*/
    
    
    create a dashboard.php and copy paste this code in starting of dashboard.php
    
    
    
    <?php
    session_start();
    include('conn.php');
    include('check_session.php');
    ?>
    
    
    
    
    /*-----------------------after that-----------------*/ 
    
    
    
    create a check_session.php which check your session and copy paste this code :- 
    
    
    <?php
        if($_SESSION["user_name"]) 
        {
    ?>
        Welcome <?php echo $_SESSION["user_name"]; ?>. Click here to <a href="logout.php" tite="Logout">Logout.</a>
    <?php
        }
        else
        {
         header("location:index.php");
        }
    ?>
    
    
    
    
    
    if you have any query so let me know on my mail id farjicompany@gmail.com
    
    0 讨论(0)
  • 2020-12-02 17:44

    Although not secure, (no offense or anything), just stick the header function after you set the session variable

     while($row = mysql_fetch_assoc($result))
        {
                $_SESSION["user"] = $username;
        }
    header('Location: /profile.php');
    
    0 讨论(0)
  • 2020-12-02 17:47

    Assuming you're using cookies for login, just call it after your setcookie call -- after all, you must be calling that one before any output too.

    Anyway in general you could check for the presence of your form's submit button name at the beginning of the script, do your logic, and then output stuff:

    if(isset($_POST['mySubmit'])) {
        // the form was submitted
    
        // ...
        // perform your logic
    
        // redirect if login was successful
        header('Location: /somewhere');
    }
    
    // output your stuff here
    
    0 讨论(0)
提交回复
热议问题