2 PHP同时切换

时间:2016-04-17 17:54:14

标签: php switch-statement case

我有一点问题,我希望你们可以帮我解决这个问题!

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

我将展示我的代码:

Register.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>

如果您需要更多信息,请询问。 我希望你们有一个解决方案,就像另一种解决方法吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以将标题位置设置为index.php?page=register,不应该转到load.php?page=register ? 或者我错过了什么?

相关问题