2015-02-11 52 views
-3

我遇到了php中的会话问题。如何在我的php代码中添加会话

这是的index.php

<form method="post" action="send.php"> 
No. HP Tujuan : <input type="text" name="nohp" value="+62"><br> 
Pesan : <textarea name="msg"></textarea><br> 
<input type="submit" name="submit" value="Kirim SMS"> 
</form> 

send.php

<?php 
mysql_connect("dbhost", "dbuser", "dbpass"); 
mysql_select_db("sms"); 

$noTujuan = $_POST['nohp']; 
$message = $_POST['msg']; 

$query = "INSERT INTO outbox (DestinationNumber, TextDecoded, CreatorID) VALUES ('$noTujuan', '$message', 'Gammu')"; 
$hasil = mysql_query($query); 
if ($hasil) echo "SMS Success"; 
else echo "SMS Failed"; 

?> 

我想创建一个会话,但我不知道怎么办。我希望send.php页面只有在用户已经完成index.php中的表单时才可以访问。

+0

你不需要会话做到这一点。只需验证是否设置了表单的变量。 'if(empty($ _ POST ['nohp'])){ehco“Please complete form”; }或将它们重定向回index.php。请参阅http://stackoverflow.com/questions/15220704/how-to-detect-if-post-is-set – JConstantine 2015-02-11 05:31:13

回答

0

解决了,谢谢你们

<?php 
 
if (isset($_POST['nohp']) AND isset($_POST['msg'])) 
 
{ 
 
    $noTujuan=$_POST['nohp']; 
 
    $message=$_POST['msg']; 
 
} 
 
else 
 
{ 
 
    die("access this page from index.php"); 
 
} 
 
    
 
if (!empty($noTujuan)) 
 
{ 
 
    echo "No. HP: $noTujuan <br /> Isi Pesan: $message"; 
 
} 
 
else 
 
{ 
 
    die(sorry, you must entry name"); 
 
} 
 
?>