将数据从textinputs发送到下一个php脚本

时间:2014-02-13 02:57:43

标签: php mysql session

这是我的php脚本http://jsfiddle.net/3BGD4/面板与此链接http://jsfiddle.net/mplungjan/HKyWY/中的按钮Zapisz相似。 我为输入做了回音$,它没有显示任何内容。有两个选项,发送错误,或texinputs错误。有什么建议?如果您不知道如何解决,请投票支持。 哦,这是我的验证脚本:

<html>
<head>
</head>
<body>
<?php
ob_start();
session_start();


$host="mysql.cba.pl"; // Host name
$username="MSobal"; // Mysql username
$password="sobalm"; // Mysql password
$db_name="msinzynierka_cba_pl"; // Database name
$tbl_name="User"; // Table name

// Connect to server and select databse.
//mysql_connect("$host", "$username", "$password")or die("cannot connect");
$link=mysqli_connect("$host", "$username", "$password");

//mysql_select_db("$db_name")or die("cannot select DB");
mysqli_select_db ($link ,  $db_name );

if (isset( $_SESSION['myusername']) && isset( $_SESSION['mypassword']) ){

$myusername = $_SESSION['myusername'];
$mypassword = $_SESSION['mypassword'];


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysqli_query($link,$sql);

}




// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

//$sql="SELECT * FROM $tbl_name WHERE username='$myusername'                 and                       
 password='$mypassword'";
//$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

//If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;

header("location:login_success.php");
}
else {
echo "Niepoprawny login lub hasło";
echo $myusername;
echo $mypassword;
}

ob_end_flush();
?>

<form action="" method="post">
Username: 
<input type="text" name="myusername">
<br>
Password: 
<input type="password" name="mypassword">
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br><br>
<a href="sessions_test_2.php">check</a>
</body>
</html>

这是login_success.php

<?php
session_start();
if ( isset( $_SESSION['myusername'] ) ){

header("location:main_login.php");
}
?>

<html>
<head>
<link href="menugora.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-2" />

</head>
<body>

<ul id="menu">
<li><a href="login_success.php?value=1">Kategorie</a></li>
<li><a href="login_success.php?value=2">Towary</a></li>
<li><a href="login_success.php?value=3">Klienci</a></li>
<li><a href="login_success.php?value=4">Towar-Zamówienie</a></li>
<li><a href="login_success.php?value=5">Zamówienie</a></li>
<li><a href="login_success.php?value=6">Wyloguj</a></li>
</ul>

<?php

$arg = (int)$_GET['value'];

switch ($arg)
{
case 1:
include('kategoria.php');
break;

case 2:
include('towar.php');
break;

case 3:
include('klient.php');
break;

case 4:
include('towarzamowienie.php');
break;

case 5:
include('zamowienie.php');
break;

case 6:
include('logout.php');
break;


}
?>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

尝试将您的html更改为:

<div class="container">
<form id="signup" action="checklogin.php" method="post">
        <div class="header">
                <h3>Logowanie</h3>
        </div>

        <div class="sep"></div>

        <div class="inputs">
                <input type="text" name="myusername" placeholder="Login" autofocus />
                <input type="password" name="mypassword" placeholder="Has&#322;o" />
                <input type="submit" id="Submit" value="Zaloguj">
        </div>
</form>
</div>

</body>

</html>

将login_success.php更改为:

<?php
session_start();
if ( !isset( $_SESSION['myusername'] ) ){

header("location:logout.php");
}
?>

<html>
<head>
<link href="menugora.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-2" />

</head>
<body>

<ul id="menu">
<li><a href="login_success.php?value=1">Kategorie</a></li>
<li><a href="login_success.php?value=2">Towary</a></li>
<li><a href="login_success.php?value=3">Klienci</a></li>
<li><a href="login_success.php?value=4">Towar-Zamówienie</a></li>
<li><a href="login_success.php?value=5">Zamówienie</a></li>
<li><a href="login_success.php?value=6">Wyloguj</a></li>
</ul>

<?php

$arg = (int)$_GET['value'];

switch ($arg)
{
case 1:
include('kategoria.php');
break;

case 2:
include('towar.php');
break;

case 3:
include('klient.php');
break;

case 4:
include('towarzamowienie.php');
break;

case 5:
include('zamowienie.php');
break;

case 6:
include('logout.php');
break;


}
?>


</body>
</html>

答案 1 :(得分:1)

编辑 - 重写(开始)

免责声明 我强烈建议您不要以纯文本存储密码,也不要使用mysql_*函数。将mysqli_*prepared statementsPDO一起使用。

在这里,我为你写了。完成登录表单,成功页面和注销页面。

(第1页,第2页,第3页)

重要 - 所有包含的文件应在顶部包含以下内容:

session_start();
if (isset($_SESSION['myusername']) && isset($_SESSION['mypassword'])){
// content
}

else{ echo "What you want to show here.";
}

否则,任何人都可以访问这些页面。

旁注:您不能将mysqli_*mysql_*功能混合使用,它们彼此不兼容。

并使用您自己的信息替换("host","username", "password", "database")

Page 1

PHP / HTML表单

<?php
ob_start();
session_start();

$link = new mysqli("xxx","username", "password", "database");
$tbl_name = "User";

if(isset($_POST['submit'])){
$myusername = mysqli_real_escape_string($link,$_POST['myusername']);
$mypassword = mysqli_real_escape_string($link,$_POST['mypassword']);

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysqli_query($link,$sql);

$count=mysqli_num_rows($result);

if($count==1){
header("location:login_success.php");
}

else{
echo "Sorry, you are not logged in.";
}

} // isset submit

ob_end_flush();
?>

<form action="" method="post">
Username: 
<input type="text" name="myusername">
<br>
Password: 
<input type="password" name="mypassword">
<br>
<input type="submit" name="submit" value="Submit">
</form>

第2页(如果已登录) - login_success.php

<?php
session_start();
if (isset($_SESSION['myusername']) && isset($_SESSION['mypassword'])){

echo "If you see this message, then you have successfully logged in.";
echo "<br>";
echo "Your account name is: ";
echo $_SESSION['myusername'];
echo "<br>";
echo "Your account password is: ";
echo $_SESSION['mypassword'];

echo "<hr>";
echo "<a href='logout.php'>Logout</a>";
}

else{
echo "Sorry, you are not logged in.";
}

第3页(退出页面) - logout.php

<?php
session_start();
session_destroy();
echo "<a href='login.php'>Log back in</a>";

编辑结束




原始答案

这是一种快速方法,用于检查用户名和密码SESSION名称在提交并传递到其他页面时是否会被转移。

侧注:使用var_dump($_SESSION['myusername']);var_dump($_SESSION['mypassword']);进行调试/排除故障。 $_SESSION可以替换为$_POST

e.g。 var_dump($_POST['myusername']);以后查看是否有任何设置。

免责声明 我强烈建议您不要以纯文本存储密码,也不要使用mysql_*函数。将mysqli_*prepared statementsPDO一起使用。

您需要填写其余内容以适合您的数据库代码。

page 1代码中,您会注意到一个链接:

<a href="sessions_test_2.php">check</a>

这将在那里显示用户名和密码SESSION名称。

表单的操作设置为action=""

第1页:

<?php
ob_start();
session_start();

if(isset($_POST['submit'])){
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;

echo $_SESSION['myusername'];
echo "<br>";
echo $_SESSION['mypassword'];

// header("location:login_success.php");

} // isset submit

ob_end_flush();
?>

<form action="" method="post">
Username: 
<input type="text" name="myusername">
<br>
Password: 
<input type="password" name="mypassword">
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br><br>
<a href="sessions_test_2.php">check</a>

第2页(sessions_test_2.php)

<?php
session_start();
if (isset($_SESSION['myusername']) && isset($_SESSION['mypassword'])){

echo $_SESSION['myusername'];
echo "<br>";
echo $_SESSION['mypassword'];
}

<强>脚注:

必须在使用会话的所有页面内使用

session_start();

在这两个jsfiddle文件中加上这些 :(如Vikas在答案中所述)

<input type="myusername" placeholder="Login" autofocus />
<input type="mypassword" placeholder="Has&#322;o" />

应该是:

<input type="text" name="myusername" placeholder="Login" autofocus />
<input type="password" name="mypassword" placeholder="Has&#322;o" />

您可以使用此方法:

$_SESSION['myusername'] = $_POST['myusername'];
$_SESSION['mypassword'] = $_POST['mypassword'];

DB

您的数据库代码如下所示:

session_start();

...

if (isset( $_SESSION['myusername']) && isset( $_SESSION['mypassword']) ){

$myusername = $_SESSION['myusername'];
$mypassword = $_SESSION['mypassword'];


...

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

...

}