【问题标题】:2 PHP switchs at the same time2个PHP同时切换
【发布时间】:2016-04-17 17:54:13
【问题描述】:

我有一个小问题,希望你们能帮我解决这个问题!

所以我添加了一个用于加载页面的 PHP 开关和一个带有操作开关的登录系统,但是当我注册时,我被重定向到索引。

我将展示我的代码:

注册.php:

//if logged in redirect to members page
if( $user->is_logged_in() ){ header('Location: index.php?page=profile'); }

//if form has been submitted process it
if(isset($_POST['Submit'])){


//very basic validation
if(strlen($_POST['username']) < 3){
    $error[] = 'Username is too short.';
} else {
    $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
    $stmt->execute(array(':username' => $_POST['username']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!empty($row['username'])){
        $error[] = 'Username provided is already in use.';
    }

}

if(strlen($_POST['password']) < 3){
    $error[] = 'Password is too short.';
}

if(strlen($_POST['passwordConfirm']) < 3){
    $error[] = 'Confirm password is too short.';
}

if($_POST['password'] != $_POST['passwordConfirm']){
    $error[] = 'Passwords do not match.';
}

//email validation
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
    $error[] = 'Please enter a valid email address';
} else {
    $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
    $stmt->execute(array(':email' => $_POST['email']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!empty($row['email'])){
        $error[] = 'Email provided is already in use.';
    }

}


//if no errors have been created carry on
if(!isset($error)){

    //hash the password
    $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);

    //create the activasion code
    $activasion = md5(uniqid(rand(),true));

    try {

        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO members (username,password,email,active) VALUES (:username, :password, :email, :active)');
        $stmt->execute(array(
            ':username' => $_POST['username'],
            ':password' => $hashedpassword,
            ':email' => $_POST['email'],
            ':active' => $activasion
        ));
        $id = $db->lastInsertId('memberID');

        //send email
        $to = $_POST['email'];
        $subject = "Registration Confirmation";
        $body = "<p>Thank you for registering at demo site.</p>
        <p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
        <p>Regards Site Admin</p>";

        $mail = new Mail();
        $mail->setFrom(SITEEMAIL);
        $mail->addAddress($to);
        $mail->subject($subject);
        $mail->body($body);
        $mail->send();

        //redirect to index page
        header('Location: index.php?page=register?action=joined');
        exit;

    //else catch the exception and show the error.
    } catch(PDOException $e) {
        $error[] = $e->getMessage();
    }

}

}
?>
    <form id='register' action='' method='post'
        accept-charset='UTF-8'>
<h3 style="width: 587px;">Register</h3>
                <?php
            //check for any errors
            if(isset($error)){
                foreach($error as $error){
                    echo '<h5 style="background-color: #FF4E4E; border-radius: 5px; border: solid #D30000; 2px ">'.$error.'</h5>';
                }
            }

            //if action is joined show sucess
            if(isset($_GET['action']) && $_GET['action'] == 'joined'){
                echo "<h2 class='bg-success'>Registration successful, please check your email to activate your account.</h2>";
            }
            ?>
<br />
<table>
<tr>
<td class="left"><label for='username' >Username*:</label></td>
<td class="mid"><input type='register' name='username' id='username' placeholder="Choose a username" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" maxlength="50" /></td>
<td class="right">TEXT</td>
</tr>
<tr>
<td><label for='email' >Your E-mail*:</label></td>
<td><input type='register' name='email' id='email' placeholder="Enter your E-mail addres" maxlength="50" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" /><td>
<td class="right">TEXT</td>
</tr>
<tr>
<td><label for='password' >Password*:</label></td>
<td><input type='register' name="password" id='password' placeholder="Choose a password" maxlength="50" /></td>
<td class="right">TEXT</td>
</tr>
<tr>
<td><label for='password' >Confirm Password*:</label></td>
<td><input type='register' name="passwordConfirm" id='password' placeholder="Choose a password" maxlength="50" /></td>
<td class="right">TEXT</td>
</tr>
</table>
<center>
<input type='submit' name='Submit' value='Register' />
</center>
</form>

Load.php(PHP页面切换)

error_reporting(0);
switch($_GET['page'])
{
case 'home':
case 'register':
case 'login':
case 'profile':
case 'logout':
    include("pages/$_GET[page].php");
    break;
default:
    include('pages/home.php');
}

index.php

<div id="content">
    <div class="rightcontent">
        <?php include "main/right-sidebar.php" ?>
    </div>
    <div class="leftcontent">
        <?php include "main/left-sidebar.php" ?>
    </div>
    <div class="midcontent">
        <?php   include "main/load.php"; ?>
    </div>
</div>

如果您需要更多信息,请询问。 我希望你们能找到解决方案,比如解决这个问题的另一种方法?

提前致谢

【问题讨论】:

  • 最好将代码发布在您的问题中,而不是在链接中,这样如果链接失效,问题仍然对其他人有用。
  • 使用 pastebin 聊天,而不是提问
  • 对不起,我是stackoverflow的新手

标签: php switch-statement case


【解决方案1】:

在那里你将标题位置设置为index.php?page=register,你不应该去load.php?page=register ? 还是我错过了什么?

【讨论】:

  • 添加了 index.php 也许你现在明白网站是如何工作的了。
  • 这确实改变了一些事情。所以你重定向到索引,应该重定向到注册?为什么不能直接重定向到注册,为什么要切换?通过执行双重重定向,您将丢失 $_POST 变量 action=joined
  • 我该如何解决这个问题?那么有没有更好的加载页面的方法?
  • 就像我提到的,不要通过索引或不使用标题位置,但使用类似 historypush 的方法不是正确的方法(即使它会起作用)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2022-01-09
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多