从PHP函数转到HTML页面 [英] Go to HTML page from PHP function
问题描述
我最近开始使用PHP,我想知道:
我有一个php文件,它只包含一个名为
sqlConnect();
,它检查用户是否还没有登录到MySQL数据库,如果是,将用户重定向到一个HTML页面,该页面是用户输入DB的用户名和密码的表单。 用户提交表单后,我想回到我离开的地方(在函数中)。
你应该知道的一件事是
$ _ SESSION
在另一个调用该函数的文件中启动。 下面是我尝试过的,但它不起作用:
connect.php:
<?php
函数sqlConnect(){
$ _SESSION ['CurrentPage'] =/php/sql/connect.php;
if(!array_key_exists('mysql_username',$ _SESSION)&&!array_key_exists('mysql_passwd',$ _SESSION)){
echo< script> window.location ='/ forms / sql_login.php';< /脚本>中;
}
$ servername =localhost;
$ username = $ _SESSION ['mysql_username'];
$ passwd = $ _SESSION ['mysql_passwd'];
$ db_name =ToolDB;
//创建连接
$ sql_connection = new mysqli($ servername,$ username,$ passwd,$ db_name);
//检查它是否已建立
if($ sql_connection-> connect_error){
$ msg = $ sql_connection-> connect_error;
echo< script> alert('Impossible de se connecteràla base dedonnéesMySQL:$ msg。');< / script>;
返回NULL;
}
else {
$ _SESSION ['mysql_username'] = $ username;
$ _SESSION ['mysql_passwd'] = $ passwd;
返回$ sql_connection;
}
}
?>
sql_connection.php:
<?php session_start(); $ url = $ _SESSION ['CurrentPage']; ?>
<!DOCTYPE html>
< html>
< head>
< meta charset =utf-8/>
< title>声音部门 - 程序制作工具(表单)< /标题>
< link rel =stylesheettype =text / csshref =/ style / style.css/>
< / head>
< body>
< header>
< nav>
< h2 style =margin-left:10px; font-family:sans-serif;>声音部门 - 程序制作工具(MySQL Login Page)< / h2>
< / nav>
< / header>
< div class =bodyElement>
< form action =<?php echo $ url?>方法= POST >
< p> Nom d'utilisateur:< input type =textname =mysql_username/>< / p>
< p> Mot de passe:< input type =passwordname =mysql_passwd/>< / p>
< p>< input type =submitname =submitvalue =Submit/>< / p>
< / form>
< / div>
< / body>
< / html>
是否有人知道如何实现?
PS:我知道我不应该以纯文本形式存储密码,但现在我的本地网络上正在运行该网站,我想我只是要修改我的密码以便将密码存储在以后更方便。
ob_end_clean();
header(Location:example.com);
exit();
将此放在您希望用户重定向的地方。
在给出输出后,Header()不允许您重定向,但
ob_end_clean()
允许您在输出。
I started PHP recently and I was wondering :
I have a php file which only contains a function called sqlConnect();
which checks that the user isn't yet logged into the MySQL DB, and if so, redirects the user to an html page which is a form for the user to enter the username and password for the DB.
After the user has submitted the form, I want to get back to the point where I left (in the function).
One thing you should know too is that the $_SESSION
is started in another file which calls this function.
Here's what I tried, yet it didn't work :
connect.php :
<?php
function sqlConnect() {
$_SESSION['CurrentPage'] = "/php/sql/connect.php";
if(!array_key_exists('mysql_username', $_SESSION) && !array_key_exists('mysql_passwd', $_SESSION)) {
echo "<script>window.location='/forms/sql_login.php';</script>";
}
$servername = "localhost";
$username = $_SESSION['mysql_username'];
$passwd = $_SESSION['mysql_passwd'];
$db_name = "ToolDB";
// create connection
$sql_connection = new mysqli($servername, $username, $passwd, $db_name);
// check that it was established
if($sql_connection->connect_error) {
$msg = $sql_connection->connect_error;
echo "<script>alert('Impossible de se connecter à la base de données MySQL : $msg.');</script>";
return NULL;
}
else {
$_SESSION['mysql_username'] = $username;
$_SESSION['mysql_passwd'] = $passwd;
return $sql_connection;
}
}
?>
sql_connection.php :
<?php session_start(); $url = $_SESSION['CurrentPage']; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Sound Department - Program Making Tool (form)</title>
<link rel="stylesheet" type="text/css" href="/style/style.css" />
</head>
<body>
<header>
<nav>
<h2 style="margin-left: 10px; font-family: sans-serif;">Sound Department - Program Making Tool (MySQL Login Page)</h2>
</nav>
</header>
<div class="bodyElement">
<form action="<?php echo $url ?>" method="post">
<p>Nom d'utilisateur : <input type="text" name="mysql_username" /></p>
<p>Mot de passe : <input type="password" name="mysql_passwd"/></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</div>
</body>
</html>
Does anybody know how to achieve that ?
PS : I know I shouldn't store the password in plain text, but the site is running on my local network for now and I thought I was just gonna modify my code to store the password in a more convenient way later.
ob_end_clean();
header("Location: example.com");
exit();
Put this at the end where you want the user to be redirected.
Header() doesn't let you redirect after an output is given, but
ob_end_clean()
allows you to redirect after an output.
这篇关于从PHP函数转到HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!