【发布时间】:2023-03-13 17:58:01
【问题描述】:
我正在尝试将“成功/失败”通知切换到我的网页。我已经在我的测试网站的几个部分成功地做到了这一点,但是我在我的登录页面上遇到了一些问题。我最初的做法是使用警报弹出窗口,它可以正常工作,但不提供我正在寻找的样式。我决定在网站的其他部分使用一直为我工作的模板,但登录是独一无二的,因为我在这里为用户建立会话。
这是我的原始登录代码,它按预期工作,但使用通用警报窗口...
<?php
session_start();
require_once '../php/connect.php';
if (isset($_POST['username']) and isset($_POST['password'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$password = mysqli_real_escape_string($link, $_POST['password']);
$result = mysqli_query($link, "SELECT * FROM planner WHERE username = '$username' and password = '$password'");
$count = mysqli_num_rows($result);
if ($count !== 1){
echo "<script> window.location.href='../default.html'; alert('Your credentials could not be validated!')</script>";
} else {
$_SESSION['username'] = $username;
}
if (isset($_SESSION['username'])){
header("Location: ../php/main.php");
} else {
echo "<script> window.location.href='../default.html'; alert('Your credentials could not be validated!')</script>";
}
}
mysqli_close($link);
?>
这是我试图开始工作但想出的代码
解析错误:语法错误,第 38 行文件意外结束....这是我的?> 关闭 php。
<?php
session_start();
require_once '../php/connect.php';
if (isset($_POST['username']) and isset($_POST['password'])){
$username = mysqli_real_escape_string($link, $_POST['username']);
$password = mysqli_real_escape_string($link, $_POST['password']);
$result = mysqli_query($link, "SELECT * FROM planner WHERE username = '$username' and password = '$password'");
$count = mysqli_num_rows($result);
if ($count !== 1){
echo "<script>
var no = window.open('', 'failure','top=250,left=500,height=200,width=350,menubar=no,scrollbars=no,toolbar=no');
no.document.write('<body bgcolor='#EFEFEF'/>');
no.document.write('</br>');
no.document.write('<p style='text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px'>Your credentials could not be verified</p></br>');
no.document.write('<div style='text-align:center'><button style='width:100px;border-style:solid;border-width:5px;border-color:#003399;position:absolute;left:35%;background-color:#003399;color:#ffcc00;font-weight:bold;font-family:Helvetica' value='Close' onclick='window.close()'>OK</button></div>');
window.location.href = '../default.html';</script>";
} else {
$_SESSION['username'] = $username;
}
if (isset($_SESSION['username'])){
header("Location: ../php/main.php");
} else {
echo "<script>
var no = window.open('', 'failure','top=250,left=500,height=200,width=350,menubar=no,scrollbars=no,toolbar=no');
no.document.write('<body bgcolor='#EFEFEF'/>');
no.document.write('</br>');
no.document.write('<p style='text-align:center;color:white;background-color:red;font-family:Helvetica;font-size:20px'>Your credentials could not be verified</p></br>');
no.document.write('<div style='text-align:center'><button style='width:100px;border-style:solid;border-width:5px;border-color:#003399;position:absolute;left:35%;background-color:#003399;color:#ffcc00;font-weight:bold;font-family:Helvetica' value='Close' onclick='window.close()'>OK</button></div>');
window.location.href = '../default.html';</script>";
}
mysqli_close($link);
?>
我很确定这与引号有关,但我尝试了几种不同的组合,但仍然出现错误。
如果我可以在 javascript 中保留所有 if、else 语句,window.open 代码在我的其他页面上效果很好。在这些页面中,我只是在需要的地方使用 PHP 标记来获取 javascript 之外的参数。
但是,当我尝试使用 $_Session 进行此操作时,它不起作用。
如果这是一个引号问题,如果有人能指出我正确的方向,我将不胜感激。如果这与会话有关,我可以使用一些帮助格式化 javascript,以便正确调用 ?_Session。
【问题讨论】:
-
似乎您缺少第一个
if的结束 } -
该错误意味着缺少右花括号,请数一下大括号
-
Do'h....好吧,删除了错误消息。现在我得到一个空白的 login.php 屏幕...
-
在您的建议之间,这是我的一个面子,而 anwerjunaid 现在一切正常......谢谢大家!!!
标签: javascript php session