重定向到索引,PHP

时间:2015-10-19 21:13:43

标签: php

我一直在为我的网站做一个注册表,但我发现我遇到了麻烦。

这是我的注册表格代码(PHP文件)

    <?php
include("../libraries/Security.php");
include("../includes/utils.inc");

$host = "localhost";
$userSQL = "root";
$pwSQL = "";
$db = "upbyte";

$con = mysqli_connect($host,$userSQL,$pwSQL,$db);

$username = Security::xss_clean($_POST['username']);
$name = Security::xss_clean($_POST['name']);
$email = Security::xss_clean($_POST['email']);
$password = Security::xss_clean($_POST['password']);

$sql = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($con,$sql);

$insert = "INSERT INTO users(name,username,password,email) VALUES('$name','$username',md5('$password'),'$email')";

if(isset($_POST['Register'])){

    if(mysqli_num_rows($result) == 1){
        echo "Sorry...This email already exist..";
    }

    if(empty($username) && empty($name)&& empty($password)&& empty($email)) {
        print('Fill all the fields!');
    }else if(mysqli_num_rows($result) == 1){
            echo "Sorry...This email already exist..";
    }else{
        mysqli_query($con,$insert);
        echo "Register Completed!";
        utils::redirectToUrl("../index.php");
    }


} ?>

现在一切都很好,但是当我提交注册表时,我将此重定向到这个: “http://localhost/upbyte/App/index.php”,我只想重定向到:http://localhost/upbyte/index.php

感谢。

2 个答案:

答案 0 :(得分:2)

嗯,这取决于,如果在upbite中的App目录之前有一个名为index.php的文件,您可能只是将其指向错误的文件位置。 尝试更改链接     utils::redirectToUrl("../index.php"); 至     utils::redirectToUrl("/../index.php"); 你可能错过了索引

答案 1 :(得分:1)

您可以使用header();重定向用户

进行定时重定向,

exit(header("Refresh: $time; URL=$page"));

进行即时重定向,

exit(header("Location: $page"));

阅读材料

How to fix "Headers already sent" error in PHP