Unable to display contense of a session

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
IChaps
php-forum Fan User
php-forum Fan User
Posts: 139
Joined: Tue Mar 01, 2016 3:07 pm

Fri May 14, 2021 6:14 am

Hello.

Could I please request some guidance, as I can't seem to get a session to display in a form.

I'm in the process of creating a profile form which will allow users to edit their details.
I've set it up so that it uses a session to sort details, and then pass the data to the form.
However the form will not display the data, and I'm unable to figure out why/

Code: Select all

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title><?= $_SESSION['settings_title'] ?></title>
    <!-- SEO Meta Tags-->
    <meta name="description" content="Cartzilla - Bootstrap E-commerce Template">
    <meta name="keywords" content="bootstrap, shop, e-commerce, market, modern, responsive,  business, mobile, bootstrap, html5, css3, js, gallery, slider, touch, creative, clean">
    <meta name="author" content="Createx Studio">
    <!-- Viewport-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Favicon and Touch Icons-->
    <link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
    <link rel="manifest" href="site.webmanifest">
    <link rel="mask-icon" color="#fe6a6a" href="safari-pinned-tab.svg">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="theme-color" content="#ffffff">
    <!-- Vendor Styles including: Font Icons, Plugins, etc.-->
    <link rel="stylesheet" media="screen" href="/vendor/simplebar/dist/simplebar.min.css"/>
    <link rel="stylesheet" media="screen" href="/vendor/tiny-slider/dist/tiny-slider.css"/>
    <!-- Main Theme Styles + Bootstrap-->
    <link rel="stylesheet" media="screen" href="/css/theme.min.css">
  </head>
  <!-- Body-->
  <body class="handheld-toolbar-enabled">
    <?= include($_SERVER['DOCUMENT_ROOT'].'/header.php'); ?>
    <?
        //include($_SERVER['DOCUMENT_ROOT'].'/user/custdetails.htm.php');
        if(session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }
    ?>



    <!-- Page Title-->
    <div class="page-title-overlap bg-dark pt-4">
        <div class="container d-lg-flex justify-content-between py-2 py-lg-3">
            <div class="order-lg-2 mb-3 mb-lg-0 pt-lg-2">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-light flex-lg-nowrap justify-content-center justify-content-lg-start">
                        <li class="breadcrumb-item">
                            <a class="text-nowrap" href="/storefront.php"><i class="ci-home"></i>Home</a>
                        </li>
                        <li class="breadcrumb-item text-nowrap">
                            <a href="/user/dashboard.php">Account</a>
                        </li>
                        <li class="breadcrumb-item text-nowrap active" aria-current="page">Profile info</li>
                    </ol>
                </nav>
            </div>
            <div class="order-lg-1 pe-lg-4 text-center text-lg-start">
                <h1 class="h3 text-light mb-0">Profile info</h1>
            </div>
        </div>
    </div>
    <div class="container pb-5 mb-2 mb-md-4">
        <div class="row">
            <!-- Sidebar-->
            <aside class="col-lg-4 pt-4 pt-lg-0 pe-xl-5">
                <div class="bg-white rounded-3 shadow-lg pt-1 mb-5 mb-lg-0">
                    <div class="d-md-flex justify-content-between align-items-center text-center text-md-start p-4">
                        <div class="d-md-flex align-items-center">
                            <div class="img-thumbnail rounded-circle position-relative flex-shrink-0 mx-auto mb-2 mx-md-0 mb-md-0" style="width: 6.375rem;">
                                <span class="badge bg-warning position-absolute end-0 mt-n2" data-bs-toggle="tooltip" title="Reward points">384</span>
                                <img class="rounded-circle" src="/img/shop/account/avatar.jpg" alt="Susan Gardner">
                            </div>
                            <div class="ps-md-3">
                                <h3 class="fs-base mb-0"><?= $_SESSION['customer_first'] ?></h3><span class="text-accent fs-sm">s.gardner@example.com</span>
                            </div>
                        </div>
                        <a class="btn btn-primary d-lg-none mb-2 mt-3 mt-md-0" href="#account-menu" data-bs-toggle="collapse" aria-expanded="false"><i class="ci-menu me-2"></i>Account menu</a>
                    </div>
                    <div class="d-lg-block collapse" id="account-menu">
                        <div class="bg-secondary px-4 py-3">
                            <h3 class="fs-sm mb-0 text-muted">Dashboard</h3>
                        </div>
                        <ul class="list-unstyled mb-0">
                            <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-orders.html"><i class="ci-bag opacity-60 me-2"></i>Orders<span class="fs-sm text-muted ms-auto">1</span></a></li>
                            <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-wishlist.html"><i class="ci-heart opacity-60 me-2"></i>Wishlist<span class="fs-sm text-muted ms-auto">3</span></a></li>
                            <li class="mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-tickets.html"><i class="ci-help opacity-60 me-2"></i>Support tickets<span class="fs-sm text-muted ms-auto">1</span></a></li>
                        </ul>
                        <div class="bg-secondary px-4 py-3">
                            <h3 class="fs-sm mb-0 text-muted">Account settings</h3>
                        </div>
                        <ul class="list-unstyled mb-0">
                            <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3 active" href="account-profile.html"><i class="ci-user opacity-60 me-2"></i>Profile info</a></li>
<!--  <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3 active" href="account-profile.html"><i class="ci-user opacity-60 me-2"></i>Change Password</a></li> -->
<!--  <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3 active" href="account-profile.html"><i class="ci-user opacity-60 me-2"></i>Change Email</a></li> -->
                            <li class="border-bottom mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-address.html"><i class="ci-location opacity-60 me-2"></i>Addresses</a></li>
                            <li class="mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-payment.html"><i class="ci-card opacity-60 me-2"></i>Payment methods</a></li>
                            <li class="d-lg-none border-top mb-0"><a class="nav-link-style d-flex align-items-center px-4 py-3" href="account-signin.html"><i class="ci-sign-out opacity-60 me-2"></i>Sign out</a></li>
                        </ul>
                    </div>
                </div>
            </aside>

            <!-- Content  -->
            <section class="col-lg-8">
                <!-- Toolbar-->
                <div class="d-none d-lg-flex justify-content-between align-items-center pt-lg-3 pb-4 pb-lg-5 mb-lg-3">
                    <h6 class="fs-base text-light mb-0">Update you profile details below:</h6>
                    <a class="btn btn-primary btn-sm" href="account-signin.html"><i class="ci-sign-out me-2"></i>Sign out</a>
                </div>
                <!-- Profile form-->
                <form>
                    <!--<div class="bg-secondary rounded-3 p-4 mb-4">
                        <div class="d-flex align-items-center"><img class="rounded" src="/img/shop/account/avatar.jpg" width="90" alt="Susan Gardner">
                            <div class="ps-3">
                                <button class="btn btn-light btn-shadow btn-sm mb-2" type="button"><i class="ci-loading me-2"></i>Change avatar</button>
                                <div class="p mb-0 fs-ms text-muted">Upload JPG, GIF or PNG image. 300 x 300 required.</div>
                            </div>
                        </div>
                    </div>  -->
                    <div class="row gx-4 gy-3">
                        <div class="col-sm-6">
                            <label class="form-label" for="account-fn">First Name</label>
                            <input name="txtfirst" class="form-control" type="text" id="account-fn" value="<?= $_SESSION['customer_first'] ?>">
                        </div>
                        <div class="col-sm-6">
                            <label class="form-label" for="account-ln">Last Name</label>
                            <input name="txtsurname" class="form-control" type="text" id="account-ln" value="<?= $_SESSION['customer_surname'] ?>">
                        </div>

                        <div class="col-sm-6">
                            <label class="form-label" for="account-ln">Company Name<small>(php)</small></label>
                            <input name="txtcompany" class="form-control" type="text" id="account-ln" value="<?= $_SESSION['customer_company'] ?>">
                        </div>



                        <div class="col-sm-6">
                            <label class="form-label" for="account-ln">Address</label>
                            <textarea name="txtaddress" rows="4" class="form-control" cols="20" value="<?= $_SESSION['customer_address'] ?>"></textarea>
                        </div>

                        <div class="col-sm-6">
                            <label class="form-label" for="account-ln">Post Code</label>
                            <input name="txtpost_code"class="form-control" type="text" id="account-ln" value="<?= $_SESSION['customer_postcode'] ?>">
                        </div>

                        <div class="col-sm-6">
                            <label class="form-label" for="account-ln">Country</label>
                            <select name="comcountry" size="1" class="form-control">
                                <?php
                                    if(session_status() !== PHP_SESSION_ACTIVE) {
                                        session_start();
                                    }
                                    $servername = $_SESSION['dbf_host'];
                                    $username = $_SESSION['dbf_user'];
                                    $password = $_SESSION['dbf_pass'];
                                    $dbname = $_SESSION['dbf_databasename'];
                                    try {
                                        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                                        // set the PDO error mode to exception
                                        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                        $results = $conn->query('SELECT * FROM country');
                                        while ($row = $results->fetch()) {
                                            $crypted_token = $row['countries'];
                                            if(preg_match("/^(.*)::(.*)$/", $crypted_token, $regs)) {
                                                // decrypt encrypted string
                                                list(, $crypted_token, $enc_iv) = $regs;
                                                $enc_method = 'AES-128-CTR';
                                                $enc_key = openssl_digest(gethostname() . "|" . ip2long($_SERVER['SERVER_ADDR']), 'SHA256', TRUE);
                                                $dec = openssl_decrypt($crypted_token, $enc_method, $enc_key, 0, hex2bin($enc_iv));
                                                unset($crypted_token, $enc_method, $enc_key, $enc_iv, $regs);
                                            }
                                            echo "<option>" . $dec . "</option>";
                                        }
                                    }catch(PDOException $e) {}
                                ?>
                                <option selected="selected"><?= $_SESSION['customer_country'] ?></option>
                            </select>
                        </div>


                        <hr>
                        <div class="col-sm-6">
                            <label class="form-label" for="account-email">Email Address</label>
                            <input name="txtemail" class="form-control" type="email" id="account-email" value="<?= $_SESSION['customer_email'] ?>" disabled>
                        </div>

                        <div class="col-sm-6">
                            <label class="form-label" for="account-phone">Account Type</label>
                            <input class="form-control" type="text" id="account-phone" value="<?= $_SESSION['customer_acctype'] ?>" disabled>
                        </div>

                        <div class="col-sm-6"></div> <!-- Used as a blank/spacer
                        <div class="col-sm-6">
                            <label class="form-label" for="account-phone">Phone Number</label>
                            <input name="txtphone" class="form-control" type="text" id="account-phone" value="<?= $_SESSION['customer_x'] ?>" required>
                        </div>
                        <div class="col-sm-6">
                            <label class="form-label" for="account-phone">Screen Name</label>
                            <input name="txtscreen" class="form-control" type="text" id="account-phone" value="<?= $_SESSION['customer_sellerusername'] ?>">
                        </div> -->
<!-- ........................................................................................................................................................................................ -->
                        <hr width="75%">
                        <div class="col-sm-6">
                            <label class="form-label" for="account-pass">New Password</label>
                            <div class="password-toggle">
                                <input name="txtnew_pass" class="form-control" type="password" id="account-pass">
                                <label class="password-toggle-btn" aria-label="Show/hide password">
                                    <input class="password-toggle-check" type="checkbox"><span class="password-toggle-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <label class="form-label" for="account-confirm-pass">Confirm Password</label>
                            <div class="password-toggle">
                                <input name="txtconfirm_pass" class="form-control" type="password" id="account-confirm-pass">
                                <label class="password-toggle-btn" aria-label="Show/hide password">
                                    <input class="password-toggle-check" type="checkbox"><span class="password-toggle-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <hr width="75%">
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
                        <div class="col-sm-6">
                            <label class="form-label" for="account-pass">New Email</label>
                            <div class="password-toggle">
                                <input class="form-control" type="password" id="account-pass">
                                <label name="txtnew_email" class="password-toggle-btn" aria-label="Show/hide password">
                                    <input class="password-toggle-check" type="checkbox"><span class="password-toggle-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <label class="form-label" for="account-confirm-pass">Confirm New Email</label>
                            <div class="password-toggle">
                                <input name="txtconfirm_email" class="form-control" type="password" id="account-confirm-pass">
                                <label class="password-toggle-btn" aria-label="Show/hide password">
                                    <input class="password-toggle-check" type="checkbox"><span class="password-toggle-indicator"></span>
                                </label>
                            </div>
                        </div>
                        <hr width="75%">


<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
                        <div class="col-12">
                            <hr class="mt-2 mb-3">
                            <div class="d-flex flex-wrap justify-content-between align-items-center">
                                <div class="form-check">
                                    <input name="chknewsletter" class="form-check-input" type="checkbox" id="subscribe_me" checked>
                                    <label class="form-check-label" for="subscribe_me">Subscribe me to Newsletter</label>
                                </div>
                                <button class="btn btn-primary mt-3 mt-sm-0" type="button">Update profile</button>
                            </div>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>






    <!-- Footer-->
    <?= include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>
    <!-- Toolbar for handheld devices (Default)-->
    <div class="handheld-toolbar">
      <div class="d-table table-layout-fixed w-100"><a class="d-table-cell handheld-toolbar-item" href="account-wishlist.html"><span class="handheld-toolbar-icon"><i class="ci-heart"></i></span><span class="handheld-toolbar-label">Wishlist</span></a><a class="d-table-cell handheld-toolbar-item" href="javascript:void(0)" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" onclick="window.scrollTo(0, 0)"><span class="handheld-toolbar-icon"><i class="ci-menu"></i></span><span class="handheld-toolbar-label">Menu</span></a><a class="d-table-cell handheld-toolbar-item" href="shop-cart.html"><span class="handheld-toolbar-icon"><i class="ci-cart"></i><span class="badge bg-primary rounded-pill ms-1">4</span></span><span class="handheld-toolbar-label">$265.00</span></a></div>
    </div>
    <!-- Back To Top Button--><a class="btn-scroll-top" href="#top" data-scroll><span class="btn-scroll-top-tooltip text-muted fs-sm me-2">Top</span><i class="btn-scroll-top-icon ci-arrow-up">   </i></a>
    <!-- Vendor scrits: js libraries and plugins-->
    <script src="/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/vendor/simplebar/dist/simplebar.min.js"></script>
    <script src="/vendor/tiny-slider/dist/min/tiny-slider.js"></script>
    <script src="/vendor/smooth-scroll/dist/smooth-scroll.polyfills.min.js"></script>
    <!-- Main theme script-->
    <script src="/js/theme.min.js"></script>
  </body>
</html>
At present I will need to tidy up coding, but I would like to ask if anyone might know where I'm goimg wrong, and if there is a more modern way of storing and displaying data?

Thank You.
Michalio
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Jul 18, 2021 1:33 pm

Sat Jul 24, 2021 3:18 pm

To start session the code should be able to modify request header (to set cookie with session id), but sending a content (at the beginning of the file you have HTML code) you are closing header and starts sending the body of request.
Solution?
Start the file with php code related to start the session, but the highly recomendated way is separate php and html, php logic in one file and html in another
Free coding lessons
https://www.php-forum.com/phpforum/viewtopic.php?t=29852
Post Reply