PHP development jumps to the login page after successful registration
In the previous section, add the <a> link to the HTML of the login page and add some simple CSS styles to jump to the registration page.
#In this section, you only need to add a JavaScript jump statement to the registered PHP code to jump to the login page for login after successful registration.
<?php //插入數(shù)據(jù)庫(kù) if(!(mysqli_query($link,$sql))){ echo "<script>alert('數(shù)據(jù)插入失敗');window.location.href='zhuce.html'</script>"; }else{ echo "<script>alert('注冊(cè)成功!去登陸');window.location.href='login.html'</script>"; } ?>
In the registration PHP code in the previous chapter, add window.location.href='login.html' after alert('registration successful') to achieve this.
Of course it is also indispensable to change alert('Registration successful') to alert('Registration successful! Log in').
The reasonable login page is login.html
Full code:
<?php session_start(); header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','root','root','test'); if (!$link) { die("連接失敗:".mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $confirm = $_POST['confirm']; $email = $_POST['email']; $code = $_POST['code']; if($username == "" || $password == "" || $confirm == "" || $email == "" || $code == "") { echo "<script>alert('信息不能為空!重新填寫');window.location.href='zhuce.html'</script>"; } elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) { echo "<script>alert('用戶名至少3位且不含非法字符!重新填寫');window.location.href='zhuce'</script>"; //判斷用戶名長(zhǎng)度 }elseif(strlen($password) < 5){ echo "<script>alert('密碼至少5位!重新填寫');window.location.href='zhuce.html'</script>"; //判斷密碼長(zhǎng)度 }elseif($password != $confirm) { echo "<script>alert('兩次密碼不相同!重新填寫');window.location.href='zhuce.html'</script>"; //檢測(cè)兩次輸入密碼是否相同 } elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) { echo "<script>alert('郵箱不合法!重新填寫');window.location.href='zhuce.html'</script>"; //判斷郵箱格式是否合法 } elseif($code != $_SESSION['authcode']) { echo "<script>alert('驗(yàn)證碼錯(cuò)誤!重新填寫');window.location.href='zhuce.html'</script>"; //判斷驗(yàn)證碼是否填寫正確 } elseif(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'"))){ echo "<script>alert('用戶名已存在');window.location.href='zhuce.html'</script>"; } else{ $sql= "insert into login(username, password, confirm, email)values('$username','$password','$confirm','$email')"; //插入數(shù)據(jù)庫(kù) if(!(mysqli_query($link,$sql))){ echo "<script>alert('數(shù)據(jù)插入失敗');window.location.href='zhuce.html'</script>"; }else{ echo "<script>alert('注冊(cè)成功!去登陸');window.location.href='login.html'</script>"; } } ?>
In this way, it can be used in conjunction with the previous login registration code , we have implemented a complete user login and registration function module.