简体   繁体   English

登录后尝试将用户重定向到链接

[英]Trying to redirect user to a link after login

All I want to do is redirect a user to my homepage a couple seconds after they log in. Here's my code 我要做的就是在用户登录几秒钟后将其重定向到我的主页。这是我的代码

<?php 
    include_once("config.php");
?>

<?php if( !(isset( $_POST['login'] ) ) ) { ?>



<!DOCTYPE html>
<html>
    <head>
        <title>Codecall Tutorials - Secured Login with php5</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>

        <header id="head" >
                <p>Codecall tutorials User Login</p>
            <p><a href="register.php"><span id="register">Register</span></a></p>
        </header>

        <div id="main-wrapper">
            <div id="login-wrapper">
                <form method="post" action="">
                    <ul>
                        <li>
                            <label for="usn">Username : </label>
                            <input type="text" maxlength="30" required autofocus name="username" />
                        </li>

                        <li>
                            <label for="passwd">Password : </label>
                            <input type="password" maxlength="30" required name="password" />
                        </li>
                        <li class="buttons">
                            <input type="submit" name="login" value="Log me in" />
                            <input type="button" name="register" value="Register" onclick="location.href='register.php'" />
                        </li>

                    </ul>
                </form>

            </div>
        </div>

    </body>
</html>

<?php 
} else {
    $usr = new Users;
    $usr->storeFormValues( $_POST );

    if( $usr->userLogin() ) {

        echo "Welcome"; 


    } else {
        echo "Incorrect Username/Password"; 

    }
}
?>

Also, it would be very nice to have a user page created after a user registers. 另外,在用户注册后创建用户页面将非常好。 I have the register code all set but this is something I would like to implement. 我已经全部设置了注册码,但这是我想要实现的东西。 Any ideas? 有任何想法吗?

This this code snippet: 此代码段:

header( 'Location: http://www.yoursite.com/new_page.html' );

Here is a reference link if you need it - http://php.net/manual/en/function.header.php 如果需要,这里是一个参考链接-http://php.net/manual/zh/function.header.php

这是PHP重定向的方法:

 header('location:index.php');

Try to add 尝试添加

header(); phpfunction to redirect your page into certain location you want. phpfunction将页面重定向到您想要的特定位置。

if( $usr->userLogin() ) {
        ob_start(); // use output buffering to avoid "header already sent error"
        echo "Welcome"; //should try to remove this if you want because its unecessary now since your redirecting your page
        header('Location: pagetoredirect');
        ob_end_flush(); //now the headers are sent

    } else {
        echo "Incorrect Username/Password"; 

    }

Source( PHP.NET ) 源码( PHP.NET

You will need to clear your html first: 您将需要先清除html:

  1. Make your top part codes into: 将您的顶部代码输入:

     <?php include_once("config.php"); if( !(isset( $_POST['login'] ) ) ) { ?> 
  2. Add header where you want to redirect: header添加到您要重定向的位置:

      if( $usr->userLogin() ) { header('Location: /path/to/new/page.php'); } else { echo "Incorrect Username/Password"; } 
  3. Another way is to use javascript: 另一种方法是使用javascript:

      if( $usr->userLogin() ) { ?> <div class="message"> Thank you for logging in. You will be redirected in few seconds. Click <a href="/path/to/new/page.php">here</a> if not redirected. </div> <script type="text/javascript"> window.location = "/path/to/new/page.php"; </script> <?php } else { echo "Incorrect Username/Password"; } 

you can redirect your page through header() like this 您可以像这样通过header()重定向页面

header("location:yoursite.com?$msg=welcome user");

and it is important to notice that header() must be called before any actual output is seen... 重要的是要注意必须在看到任何实际输出之前调用header()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM