0

I have build a Website with a Contact Form. The Form is working, but I have errors where I using $_SESSION[...].

I became this Error from my STRATO Webserver:\

Notice: Undefined variable: _SESSION in /mnt/web014/c1/51/511046951/htdocs/it-test/senden_ag_best.php on line 9
Notice: Trying to access array offset on value of type null in /mnt/web014/c1/51/511046951/htdocs/it-test/senden_ag_best.php on line 9

Here is the File senden_ag_best.php, there doesn't work:

<?php
    echo '<pre>' . print_r($_SESSION,true) . '</pre>';
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    
    $vn = $_SESSION['vn'];
    $nn = $_SESSION['nn'];
    $kn = $_SESSION['kn'];
    $mail = $_SESSION['mail'];
         
    $email = new PHPMailer();
    $email->CharSet ="UTF-8";
    $email->IsSMTP();
    $email->Host = "smtp.strato.de";
    $email->SMTPAuth = true;
    include 'mail.php';
    $email->addAddress($mail);
    $email->AddEmbeddedImage('bilder/logo.png', 'logo_2u'); 
    $email->isHTML(true);
    $email->Subject ="Kontaktersuchen";
    if($_SESSION['ar'] =="f"){
        $email->Body = '...';
    }
    else{
        $email->Body = '...';
    }
               
    if($email->send()){
        ?>
        <script type="text/javascript" language="Javascript"> 
            alert("Danke für Ihre Nachricht. Wir kümmern uns darum");
        </script>
        <?php
        echo '<script type="text/JavaScript">document.location.href = "index.php?page=arbeitgeber";</script>';
        exit();
    }
    else {
        echo "es ist etwas mit dem Senden der Email schief gegangen. Bitte kontaktieren Sie den Serveradministrator unter: ...\nError: ".$email->ErrorInfo;
    }
?>

index.php:

<?php
ob_start();

session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style.css" type="text/css"/>
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300&display=swap" rel="stylesheet">
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <script src="https://kit.fontawesome.com/dd54256b88.js" crossorigin="anonymous"></script>
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script src="javascript/goto_termin.js"></script>
        <script src="javascript/goto_kontakt.js"></script>
        <script src="javascript/goto_2.js"></script>
        <script type="module">
            
            import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js";
            import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-analytics.js";
         
            const firebaseConfig = {
                ...
            };

            // Initialize Firebase
            const app = initializeApp(firebaseConfig);
            const analytics = getAnalytics(app);
        </script>
        <link rel="shortcut icon" type="image/x-icon" href="favicon.ico"/>
        <meta name="keywords" content="...">

        <?php
        if(!isset($_COOKIE['allow_cookies'])){
            ?><script src="javascript/cookie-popup.js"></script><?php
        }
        ?>
    </head>
    <?php
        $page = $_GET['page'];
        if(!isset($_COOKIE['allow_cookies'])){
            include 'cookie.html';
        }
        if($page == 'home'){
            require_once('home.php');
        }
        else if($page == 'datenschutz'){
            require_once('datenschutz.php');
        }
        else if($page == 'impressum'){
            require_once('impressum.php');
        }
        else if($page == 'datenschutz'){
            require_once('datenschutz.php');
        }
        else if($page == 'ueber'){
            require_once('ueber.php');
        }
        else if($page == 'unsere-lei'){
            require_once('unsere-lei.php');
        }
        else if($page == 'arbeitnehmer'){
            require_once('arbeitnehmer.php');
        }
        else if($page == 'arbeitgeber'){
            require_once('arbeitgeber.php');
        }
        else if($page == 'kontakt_form'){
            require_once('kontakt_form.php');
        }
        else if($page == 'termin'){
            require_once('termin.php');
        }
        else if($page == 'call_back'){
            require_once('call_back.php');
        }
        else if($page == 'inbau'){
            require_once('inbau.php');
        }
        else if($page == 'senden_ag'){
            require_once('senden_ag.php');
        }
        else if($page == 'senden_ag_best'){
            require_once('senden_ag_best.php');
        }
        else if($page == 'senden_an'){
            require_once('senden_an.php');
        }
        else if($page == 'senden_an_best'){
            require_once('senden_an_best.php');
        }
        else if($page == 'senden_cb'){
            require_once('senden_cb.php');
        }
        else if($page == 'senden_cb_best'){
            require_once('senden_cb_best.php');
        }
        else if($page == 'senden_kf'){
            require_once('senden_kf.php');
        }
        else if($page == 'senden_kf_best'){
            require_once('senden_kf_best.php');
        }
        else if($page == 'senden_tf'){
            require_once('senden_tf.php');
        }
        else if($page == 'senden_tf_best'){
            require_once('senden_tf_best.php');
        }
        else if($page == '401'){
            require_once('401.php');
        }
        else if($page == '403'){
            require_once('403.php');
        }
        else if($page == '404'){
            require_once('404.php');
        }
        else if($page == '500'){
            require_once('500.php');
        }
    ?>
</html>

I call all from the index.php inclusive the senden_ag_best.php. When I add session_start(); in the senden_ag_best.php then I have the Error:

Warning: session_start(): Cannot start session when headers already sent in /mnt/web014/c1/51/511046951/htdocs/it-test/senden_ag_best.php on line 2

to call the page I'm using:

.../index.php?page=senden_ag_best

How I set the Sessions in the Contact Form:

$vn = $_POST['vn'];
$vn = str_replace("ü", "&uuml;", $vn);
$vn = str_replace("ß", "&szlig;", $vn); 
$vn = str_replace("ö", "&ouml;", $vn); 
$vn = str_replace("Ü", "&Uuml;", $vn);
$vn = str_replace("Ä", "&Auml;", $vn);
$vn = str_replace("Ö", "&Ouml;", $vn);
$_SESSION['vn'] = $vn;

Simple Example to test the Sessions: 1.php:

<?php
session_start();
$test = '1';
$_SESSION['test'] = $test;
header('Location: 2.php');
?>

2.php:

<?php
session_start();
echo $_SESSION['test'];
echo '<pre>' . session_status() . '</pre>';
?>

Please help me to solve this Error. Thanks in advance

Technofox
  • 9
  • 3
  • So `$vn = $_SESSION['vn'];` is line 9 is it? Does `senden_ag_best.php` have an `include` or `require` in it to bring in the `index.php` script where you start the session? I'm guessing not (and it probably doesn't make sense to do so, if that's actually a separate page of your site). If not then you'll need to run `session_start()` again in `senden_ag_best.php` so that it can also read from the Session. – ADyson Feb 08 '22 at 16:30
  • 1
    Can you show all the relevant code in context and in the correct sequence it occurs, not just random snippets in lines. It's unclear too - is the page you're executing index.php with senden_ag_best.php required within it, or the other way round? Because the error is clear - you definitely haven't started the session when you try to use it. So either you haven't included the relevant file properly, or you're trying to access it before the session starts, something like that. It's hard to tell exactly what's going on from the way it's presented here right now. – ADyson Feb 08 '22 at 16:38
  • Ok, so looks like we were both right. There is no `session_start();` in `senden_ag_best.php` Do you read the comments? – RiggsFolly Feb 08 '22 at 16:43
  • Thanks for the update. So, are you running this code by going to `index.php?page=senden_ag_best` in your browser? Or some other way? – ADyson Feb 08 '22 at 16:45
  • `Cannot start session when headers already sent` will happen if there has been some output to the HTTP response (e.g. some HTML content or output from `echo`) before the session_start() command occurs. So I guess that's from when you've used the senden_ag_best page via the require_once, because lots has already happened by then. – ADyson Feb 08 '22 at 16:49
  • To be honest though, if you're calling `index.php?page=senden_ag_best` as the URL then I don't think this code would reproduce the problem. I mean it's possible those specific Session variables might not be defined, but I can't see how $_SESSION would entirely fail to exist, since session_start() would be called at the start of the index.php script – ADyson Feb 08 '22 at 16:50
  • 1
    @ADyson Obviously not :) Sorry, will start again – RiggsFolly Feb 08 '22 at 16:52
  • 1
    Do any of the other REQUIRE'd scripts also have a problem with the SESSION or do they not use it? – RiggsFolly Feb 08 '22 at 16:58
  • @RiggsFolly I have 4 other Contact Forms there are structurally identical with this – Technofox Feb 08 '22 at 17:01
  • It might also be useful to know which script sets these values into the `$_SESSION` array. Did you try printing the session for debugging purposes like `echo '
     . print_r($_SESSION,true) . '
    ';`
    – RiggsFolly Feb 08 '22 at 17:01
  • _I have 4 other Contact Forms there are structurally identical with this_ Ok, but have you tested them and do they work with the session or not ? :) – RiggsFolly Feb 08 '22 at 17:02
  • @RiggsFolly the answer from you Debug Idea: Notice: Undefined variable: _SESSION in /mnt/web014/c1/51/511046951/htdocs/it-test/senden_ag_best.php on line 2 and yes all other doesn't work – Technofox Feb 08 '22 at 17:04
  • **Line 2**?? Is a `use` statement?? Have you changed the code – RiggsFolly Feb 08 '22 at 17:06
  • yes in this LINE 2 is now your Debug Code – Technofox Feb 08 '22 at 17:08
  • If I didnt know any better, I would think something is closing the session somewhere in the code before you require the other script – RiggsFolly Feb 08 '22 at 17:17
  • Grasping at straws a bit here, can you replace the previous debug line with `echo '
    ' . session_status() . '
    ';`
    – RiggsFolly Feb 08 '22 at 17:24
  • do you have a other idea where I can transfer the data from the Contact form to senden_ag_best.php? – Technofox Feb 08 '22 at 17:25
  • @RiggsFolly the session status is 1 – Technofox Feb 08 '22 at 17:26
  • That means there is no session active – RiggsFolly Feb 08 '22 at 17:27
  • UFF. How I can solve this? – Technofox Feb 08 '22 at 17:28
  • ok, we now have to check that there are no spaces in the code before the first ` – RiggsFolly Feb 08 '22 at 17:35
  • ok. Do you have alternates instead of Sessions? – Technofox Feb 08 '22 at 17:40
  • 1
    Sessions work for millions of sites, can you try a simple example, remove all the complexity and see if that works. That may point us to some setting. Did you purchase a complete hosting package or are you using a cheap one – RiggsFolly Feb 08 '22 at 17:46
  • there is a complete Hosting Package(Hosting Plus) – Technofox Feb 08 '22 at 17:49
  • I build a very very simple example and I have the same Error and the session status is again 1 – Technofox Feb 08 '22 at 17:54
  • @RiggsFolly I checked my php preferences and the Strato Server disabled `register_globals`. Is this that produce the Error? – Technofox Feb 10 '22 at 06:54
  • 1
    NO!! That was a dangerous option that hes been turned OFF for years and shoudl definitely STAY TURNED OFF – RiggsFolly Feb 10 '22 at 12:06
  • Can you add your simple example to your question. Maybe we can see something more when there is less noise around the important bits – RiggsFolly Feb 10 '22 at 12:07
  • @RiggsFolly example is in the question – Technofox Feb 10 '22 at 15:56
  • `2.php` does not have a `session_start()` in it. EVERY script that uses the session MUST have one – RiggsFolly Feb 10 '22 at 15:58
  • I have a `session_start();` – Technofox Feb 10 '22 at 15:59
  • Oh, and now it does! Ok, so do the simple scripts work or not? – RiggsFolly Feb 10 '22 at 15:59
  • it works! But why do this works and the other not? Could it be the index.php? – Technofox Feb 10 '22 at 16:06
  • Can you look at your `php.ini` file and tell me what is the `session.save_handler` parameter is set to please – RiggsFolly Feb 10 '22 at 16:10
  • Ok, but that does not help me, I am not a STRATO customer and therefore _have no idea what the standard `php.ini` file looks like_ – RiggsFolly Feb 10 '22 at 16:17
  • PHP: 7.4 the session save handler is "files". – Technofox Feb 10 '22 at 16:18
  • Then I have run out of ideas, the only thing I can think of is that something you are incuding somewhere is causing SESSION issues, but I have no idea what that may be. – RiggsFolly Feb 10 '22 at 16:21
  • https://ibb.co/jhbQtG4 `phpinfo();` – Technofox Feb 10 '22 at 16:22
  • @RiggsFolly It works!!!!!!!!! :) I tried do redirect all without the index.php – Technofox Feb 10 '22 at 16:43

0 Answers0