【发布时间】:2012-10-17 11:15:09
【问题描述】:
如果值的格式不正确,用于处理我的表单操作的脚本将重定向到表单页面。我想用用户在重定向到表单页面时输入的错误数据填充文本字段和文本区域。我编写了以下脚本,该脚本在错误值提交时重定向页面,但此后不填写字段。
表单页面上的脚本:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
表单的html部分:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
addscript.php 上提交表单值的重定向脚本:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
请告诉我问题出在哪里以及如何解决问题? 提前致谢。
【问题讨论】:
-
我发现的一个快速问题是 session_id('stat')=="true",session_id() 通常是用于安全的随机字符串,你在上面使用了它但是 $_SESSION[ 'stat']= "true" 下面。顺便说一句,如果你想使用布尔值(true 或 false),它们不需要被引用,否则它们是字符串,而不是布尔值。
-
你看起来对
session_id函数的用途非常误解。使用前请阅读手册:php.net/session_id - 您的意思可能是session_start()。另见php.net/session -
在上面两个cmets的基础上,如果你在页面上使用了session,你应该在每个php页面的顶部写上session_start()。如果你忘记了,请添加它
标签: php forms session redirect