Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code:
<?php
  require_once 'init.php';
  include 'includes/head.php';
  include 'includes/navigation.php';

  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

  $error = false;
  $msg = "";
  $msg2 = "";
  $msg3 = "";
if($_POST){
  $name = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '',
  (isset($_POST['name']) && $_POST['name'] != ''?sanitize($_POST['name']):''));
  $phone = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '',
  (isset($_POST['phone']) && $_POST['phone'] != ''?sanitize($_POST['phone']):''));
  $email = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '',
  (isset($_POST['email']) && $_POST['email'] != ''?sanitize($_POST['email']):''));
  $message = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '',
  (isset($_POST['message']) && $_POST['message'] != ''?sanitize($_POST['message']):''));
  include_once 'PHPMailer/Exception.php';
  include_once 'PHPMailer/PHPMailer.php';
  include_once 'PHPMailer/SMTP.php';
  $mail = new PHPMailer();
  $mail->CharSet = 'UTF-8';
  $mail->IsSMTP();
  $mail->Mailer = 'smtp';
  $mail->SMTPAuth = true;
  $mail->Host = '';
  $mail->Port = 465;
  $mail->SMTPSecure = 'ssl';
  $mail->addAddress("");

  $mail->Username = "";
  $mail->Password = "";

  $mail->IsHTML(true); 
  $mail->SingleTo = true;

  $mail->setFrom($email, $email);
  $mail->Subject = "Order";
  $mail->Body = "<style>
                 h5{
                 background-color: #eaeaea;
                 padding-top: 8px;
                 padding-bottom: 8px;
                 font-weight: bold;
                 text-decoration: underline;
                 text-align: center;
                 margin-bottom: 10px;
                 }
                 table{
                   border-collapse: collapse;
                   width: 100%;
                 }
                 td, th{
                   border: 1px solid #dddddd;
                   text-align: left;
                   padding: 8px;
                 }
                 </style>
                 <h5>User Details</h5>
                 <table>
                 <tr>
                 <th>Name</th>
                 <td>$name</td>
                 </tr>
                 <tr>
                 <th>Email</th>
                 <td>$email</td>
                 </tr>
                 <tr>
                 <th>Phone</th>
                 <td>$phone</td>
                 </tr>
                 <tr>
                 <th>Message</th>
                 <td>$message</td>
                 </tr>
                 </table>";

//form validation
if(empty($_POST['name']) || empty($_POST['phone']) || empty($_POST['email']) || empty($_POST['message'])){
  $error = true;
  $msg = "<ul><li class='text-danger'>Please fill all the blanks.</li></ul>";
}

if(!filter_var($email,FILTER_VALIDATE_EMAIL) && !empty($_POST['email'])){
    $error = true;
    $msg2 = "<ul><li class='text-danger'>Please enter a valid email.</li></ul>";
}

if($error === false && $mail->Send()){
  $_SESSION['msg'] = "<p class='text-success'>Email has been sent.</p>";
  header('Location: contact.php');
 }
}
  ?>


        <?=(isset($msg)?$msg:'');?>
        <?=(isset($msg2)?$msg2:'');?>

        <?php
         if(isset($_SESSION["msg"])){
           echo $_SESSION['msg'];
             unset($_SESSION['msg']);
           }
         }


What I have tried:

The code works well except the session message.
PHP
session_start();
is in init.php. I am trying to display a message using session in the same page, but the message is never shown...

I tried removing
PHP
unset($_SESSION['msg']);
but I faced a new problem which is the message will not be removed when refreshing the page or moving to another page. It will stay there unless I restart dns (WAMP). How to fix this issue?
Posted
Updated 28-May-23 2:33am
Comments
Member 15627495 28-May-23 8:45am    
Hello !

1 - Which statement is the better ? => require_once !!!!!
// include() and require() are deprecated since few years, because of security breachs.

// go by 'require_once()' every time you have to import an external resource.
// require_once() will load the resource one time instead of multiple times.
// that a good reason to use only 'require_once()'.


2 - What are $_session Vars ?
// $_SESSION is an Array with values in, and all those values are sent by url, to the client.
// you can set them, and get them back at each page call.
// $_SESSION are transmit from server to client, and from client to server, it's 2 direction you can use.
// all the $_SESSION are in the url fields with both names and values.


3- Do you use global keyword to code ?
// the 'global' make a var reachable all over your php script and scopes.
// put it at top of your code page, and use your var every where you need them.
<?php
 global $var_one, $var_two ;
?>
// by this keyword you make your vars as 'shared' through all your php pattern.

1 solution

You must run 'session_start();' on each page where you use sessions. 'session_start();' must be the first line of code in your php page -

//Check of a session is running on this page...
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once 'init.php';
  include 'includes/head.php';
  include 'includes/navigation.php';

  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

//Rest of your code...
 
Share this answer
 
v2
Comments
FRS4002 29-May-23 3:20am    
I already said that session_start(); is in init.php and it works well ...
Andre Oosthuizen 29-May-23 7:09am    
If your init.php page is a session class, where 'msg' is declared, please show that code - If you want to handle sessions with a class[^]. You are setting a value to 'msg' - '$_SESSION['msg'] = "Email has been sent.";' in your If statement which can return a false and msg will be empty. You are then checking if the msg has been set (It was not) and echo the value out, it will be empty.
if(isset($_SESSION["msg"])){
           echo $_SESSION['msg'];
             unset($_SESSION['msg']);


To see if your 'msg' does have a value, try -
if($error === false && $mail->Send()){
  $_SESSION['msg'] = "<p class='text-success'>Email has been sent.</p>";
//Show the value of the session msg...
echo ($_SESSION['msg']);
  header('Location: contact.php');
 }


I could not see any calls made to your class in init.php (assuming it has a session class), you thus have to add session_start to your email page.
FRS4002 29-May-23 7:23am    
This code is in init.php
<?php
ob_start();
session_start();
?>

Also, I tried to echo session msg but nothing happened... But why it works when I remove unset? How to fix this issue?
Andre Oosthuizen 29-May-23 7:48am    
There is no class in init.php which means you have to add 'session_start' to your email page, your code should work fine then unless there are other error messages.
FRS4002 29-May-23 8:17am    
I added session_start it says:
Notice: session_start(): Ignoring session_start() because a session is already active
There is a session start in init.php I said that multiple times ... and it is working fine. Also, I am not getting any errors... I don't know where the problem is...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900