Cookie-Stored Setting Not Applying

I am making a setting in my WooCommerce website where the user can choose between a table view and a grid view of the products. I use cookies for this. The setting works sometimes when clicking on it; but when changing page, when there is pagination, it doesn’t always retain the selected view.

I set the cookie like this:

document.cookie = "Techsaker_se_vyval=Rutnätsvy";

And then, in my content-product.php file I check it like this:

// If products view not set to grid, show table view
if (($_COOKIE['Techsaker_se_vyval']) != 'Rutnätsvy') {

It seems the cookie value isn’t retained.

Additionally, it will sometimes also show the table view on one page of a product category, and then the grid view on a different page.

I have also observed that if I print the cookie value to the console with console.log, the value is often different from when I print it from the php with:

var_dump($_COOKIE['Techsaker_se_vyval']);

For example when I tested this, they showed conflicting values on page 1 of my shop startpage and the same values on page/2/. And I could only change the value successfully on page 2.

After further tests, I observed that the JavaScript and php sometimes do show the same value also on page 1, but even then, I couldn’t change the view on page 1. On clicking on an option, the JavaScript value in the console sometimes changes without changing the view printed, sometimes not even that. However; in a product category, as opposed to the shop startpage, I have seen that sometimes I can change the view on both page 1 and page 2.

It’s always the php value that determines what view is output. Why isn’t it always registering the same value as the JavaScript?

And changing the view always seems to work when it’s on page 2.

Does anyone know why this could be happening?

Thank you!

Why does if($x = 1 && $x == 1) throw error but if ($x == 1 && $x = 2) doesn’t in php?

In php the following code throws error:

if($x = 1 && $x == 1)

I’ve been told, it’s because && has higher precedence than =, which cases the expression to get converted to:

if($x = (1 && ($x == 1)))

So far so good, but now consider:

if $x=1; ($x == 1 && $x = 2)

This doesn’t throw error. Why doesn’t it get converted to:
if $x=1; (($x == 1) && $x) = 2)

I’ve been told thats due to = being right assosiative, but https://www.php.net/manual/en/language.operators.precedence.php says When operators have equal precedence their associativity decides how the operators are grouped.. Here we have =, && and == all being of different precedence.

P.S; My actual code is if($result != false && $res = $stmt->get_result()), which has been copied from some other reputable source, so seems like not using unneeded parenthesis is common in php.

how do I prevent form from checking at the current session?

enter image description here

enter image description here

The ones checked are the correct answer to the question. But when I click on the submit button, the questions changes and state the answers are wrong. I’ve tried too fix it by using the GET method. But that didn’t work either.

The result I am trying to get it have 3 random questions. Which are stored in an array with it’s options and answer. When the 3 questions are displayed, the options will be displayed with it. When the user clicks on the correct answer it should display the correct result, but it is not able to at the moment.

Let me include the code below.

    <?php
    /*-----------------------------------Generate 3 Random Questions ----------------------------------------- */
    /*Array Random Function*/
    $pickqns_key = array_rand($qnPool,3);
    //var_dump($pickqns_key); ----- Test if Array is sucessfully randomise with 3 questions

    /*Store 3 Question into an Array using the key generated*/
    $qns_3 = array();
    $i = 0;

    foreach($pickqns_key as $key){
        $qns_3[$i] = $qnPool[$key];
        $i++;
    }
    /*Individual Question List*/
    $question1 = $qns_3[0]["qns"];
        $q1option1 = $qns_3[0]["ops"][0];
        $q1option2 = $qns_3[0]["ops"][1];
        $q1option3 = $qns_3[0]["ops"][2];
            $q1ans = $qns_3[0]["ans"];
            var_dump($qns_3[0]);
            var_dump($q1ans);
    $question2 = $qns_3[1]["qns"];
        $q2option1 = $qns_3[1]["ops"][0];
        $q2option2 = $qns_3[1]["ops"][1];
        $q2option3 = $qns_3[1]["ops"][2];
            $q2ans = $qns_3[1]["ans"];
            var_dump($qns_3[1]);
            var_dump($q2ans);
    $question3 = $qns_3[2]["qns"];
        $q3option1 = $qns_3[2]["ops"][0];
        $q3option2 = $qns_3[2]["ops"][1];
        $q3option3 = $qns_3[2]["ops"][2];
            $q3ans = $qns_3[2]["ans"];
            var_dump($qns_3[2]);
            var_dump($q3ans);
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Final Exam</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1"><!--Please remember to change style sheet for each page-->
        <link rel= "stylesheet" href="styles/qn_History.css" type="text/css"/> 
    </head>
    <body>
        <header>
            <img class="logo" src ="source/images/nav_logo.png" alt="logo">
                <!------------Nav Bar-------------------->
                <nav>
                <ul class ="nav__links">
                  <li><a href="index.php">Home</a></li>
                  <li><a href="qn_History.php">History</a></li>
                  <li><a href="qn_Math.php">Mathematics</a></li>
                  <li><a href="#">Contact Us</a></li> <!--This contact us page is stated here just for show. If we have time we can create a simple form that doesn't work-->
                </ul>
                </nav>
        </header>
        
        <!-- This is where the main content for your webpages are going to be-->
        <div class ="quiz_box">
            <h2>History Questions - Answer Qn 1 - 3</h2>
            <div class="std_id">
                <?php
                    //$student_ID = $_GET['studentNum'];
                    //echo "<h3>Student No: $student_ID </h3>"; 
                ?>
            </div>
            <div class="history_qn">
                <form action="qn_History.php" method="POST">
<!------------------------------------History - Question 1 ------------------------------------------------------------->
                    <div class="qn">
                       <?php 
                            /*1st Trial at Printing Question and Radio Button*/
                            //$qn_counter = 1;
                            /*Printing out each Question from the Array*/
                            //foreach($qns_3 as $qns_No => $value){
                                //echo $qn_counter,". ",$value['qns'],"<br>";
                                /*Printing Out Radio Buttons of Options8*/
                                /*foreach($value['ops'] as $options){
                                    echo '<input type="radio" name= $qns_3 = $options>',$options,"<br>";
                                }
                                $qn_counter++;
                            }*/
                        ?>
                        <p>1. <?php echo $question1 ?><p></p>
                        <!--Option 1 -->
                        <input type ="radio" id ="q1o1" name = "qn1" value ="<?php echo $q1option1?>"/>
                        <label for ="q1o1"><?php echo $q1option1 ?></label><br>

                        <!--Option 2 -->
                        <input type ="radio" id ="q1o2" name = "qn1" value ="<?php echo $q1option2?>"/>
                        <label for ="q1o2"><?php echo $q1option2 ?></label><br>

                        <!--Option 3 -->
                        <input type ="radio" id ="q1o3" name = "qn1" value ="<?php echo $q1option3?>"/>
                        <label for ="q1o3"><?php echo $q1option3 ?></label><br>

                        <!-- Qn3 Action Handler-->
                        <div class = "action_handler">
                            <?php
                                $counter = 0;
                                if(isset($_POST['qn1'])){
                                    $ans1 = $_POST['qn1'];
                                    var_dump($ans1);
                                }
                                else{
                                    $ans1 = NULL;
                                }

                                if($ans1 != NULL){
                                    if($ans1 != $q1ans){
                                        echo "$ans1 is incorrect";

                                    }
                                    else{
                                        echo "$ans1 is correct";
                                        $counter += 1;                                        
                                    }
                                }
                                else{
                                    echo "Select an answer for Qn.1 <br>";
                                }
                            ?>
                        </div>
                    </div>
                    <br>

<!------------------------------------History - Question 2 ------------------------------------------------------------->
                    <div class="qn">
                        <p>2. <?php echo $question2 ?></p>
                        <!--Option 1 -->
                        <input type ="radio" id ="q2o1" name = "qn2" value ="<?php echo $q2option1?>"/>
                        <label for ="q2o1"><?php echo $q2option1 ?></label><br>

                        <!--Option 2 -->
                        <input type ="radio" id ="q2o2" name = "qn2" value ="<?php echo $q2option2?>"/>
                        <label for ="q2o2"><?php echo $q2option2 ?></label><br>

                        <!--Option 3 -->
                        <input type ="radio" id ="q2o3" name = "qn2" value ="<?php echo $q2option3?>"/>
                        <label for ="q2o3"><?php echo $q2option3 ?></label><br>
                        <div class="action_handler">
                            <?php
                                if(isset($_POST['qn2'])){
                                    $ans2 = $_POST['qn2'];
                                    var_dump($ans2);
                                }
                                else{
                                    $ans2 = NULL;
                                }
                            
                                if($ans2 != NULL){
                                    if($ans2 != $q2ans){
                                        echo "$ans2 is incorrect";
                                    }
                                    else{
                                        echo "$ans2 is correct";
                                        $counter += 1;
                                    }
                                }
                                else{
                                    echo "Select an answer for Qn.2 <br>";
                                }
                            ?>
                        </div>
                    </div>
                    <br>

<!------------------------------------History - Question 3 ------------------------------------------------------------->
                    <div class="qn">
                    <p>3. <?php echo $question3 ?></p>
                        <!--Option 1 -->
                        <input type ="radio" id ="q3o1" name = "qn3" value ="<?php echo $q3option1?>"/>
                        <label for ="q3o1"><?php echo $q3option1 ?></label><br>

                        <!--Option 2 -->
                        <input type ="radio" id ="q3o2" name = "qn3" value ="<?php echo $q3option2?>"/>
                        <label for ="q3o2"><?php echo $q3option2 ?></label><br>

                        <!--Option 3 -->
                        <input type ="radio" id ="q3o3" name = "qn3" value ="<?php echo$q3option3?>"/>
                        <label for ="q3o3"><?php echo $q3option3 ?></label><br>
                        <div class="action_handler">
                        <!-- Qn3 Action Handler-->
                            <?php
                                if(isset($_POST['qn3'])){
                                    $ans3 = $_POST['qn3'];
                                    var_dump($ans3);
                                }
                                else{
                                    $ans3 = NULL;
                                }
                            
                                if($ans3 != NULL){
                                    if($ans3 != $q3ans){
                                        echo "$ans3 is incorrect";
                                    }
                                    else{
                                        echo "$ans3 is correct";
                                        $counter += 1;
                                    }
                                }
                                else{
                                    echo "Select an answer for Qn.3 <br>";
                                }
                            ?>
                        </div>
                    </div>
                    <!--End of History Quiz -->
                    <br>
                        <input type="submit" name="submit" value="Submit Quiz" class ="btn"/>
                        <a href="resultpage.php" class="result-btn">View results</a>
                    </div>
                <div class ="results">
                    <?php
                        echo "<h3>Your score for this History Quiz is $counter / 3</h3>";
                    ?>
                </div>
                </form>
            </div>

How to protect a direct download link in a Laravel API

I’m new to Laravel and I’m trying to protect some file downloads trough Laravel.

All I’ve read about it is about sending files as “blob” but this solution has issues when I download Blob in my React App: some files stop the download before the entire file is received.

The methode I used

return response()->download($file, 'Test File', $headers, 'inline');

Thanks for the help

How can I merge just parts of a PHP array?

I have a system that should forward incomming SMS to email. (Currently the script just forwards the file as is… Not very elegant.)
This system deals with a text file with multiple lines. I read it with file() directly into an array. The top few lines have a format as below, and I want to pick some parts out and format them into the email. The message is at the bottom of this text file, and may be over several line including blank ones.

How do I merge just the last few parts (message lines) of the array not knowing the number of lines that it has in total? I think I know which line to start at…

From: 479064xxxx
From_TOA: 91 international, ISDN/telephone 
From_SMSC: 479200xxxx
Sent: 22-02-09 13:34:05
Received: 22-02-09 12:34:29
Subject: GSM1
Modem: GSM1
IMSI: 2420266xxxxxxxx
IMEI: 8643940xxxxxxxx
Report: no
Alphabet: ISO
Length: 5

Actual message comes here!

How to check if at last post and navigate to first post

I have page arrows (Previous and Next ) on every single post however when I reach the last post I want to still keep the Next button which will link to the first post and vice versa if I’m on the first post I want to keep the previous button which would link to last post.

This is my .twig file which currently simply navigates to next or previous post if there is one.

<div class="post-prev">
    {% if post.prev %}
          <a href="{{ post.prev.link }}">Previous</a>
    {% endif %}
</div>
<div class="post-next">
    {% if post.next %}
          <a href="{{ post.next.link }}">Next</a>
    {% endif %}
</div>

and my PHP file which orders posts by date.

$context = Timber::get_context();
$context[ "main_menu" ] = new TimberMenu('main');

$context['page'] = new TimberPost();

$args = array(
    // Get post type project
    'post_type' => 'post',
    // Get all posts
    'posts_per_page' => -1,
    // Order by post date
    'orderby' => array(
        'date' => 'DESC'
    )
);

Dynamic Loading Interface Until The Data Is Processed

I am writing a php program that takes URL as input from the users and finds out the dead links from that webpage of the URL. Sometimes, if the webpage is too big then my execution speed of the program takes some more time. So how can I make a dynamic interface like loading (until the data program is fully executed)?

Symfony 5: 500 errors are not catched in prod environment

I have a strange problem in a symfony 5.3.10 app and i can’t wrap my head around it.

If i trigger a 500 error, like so:

/**
 * @Route("/", name="pvr_index", methods={"GET"})
 */
public function index(): Response
{
    echo $a;

Where $a is not defined, in dev environment i get a nice error page.

I’d expect that in prod i would get a not so nice error page that just tells me that i have a 500 error. And this would happen correctly until some time ago, when something changed, but i don’t know what.

Now instead i get no error, not even in the symfony server console, and the script execution would keep on going like nothing happened.

What i tried:

Adding the supposed exception inside a try/catch:

try {
        echo $a;
    }
    catch (Exception $e)
    {
        dump($e);
        die();
    }

Nothing happens, the catch isn’t triggered.


Creating an exception listener:

In services.yaml i have:

exception_listener:
    class: AppEventExceptionListener
    tags:
        - { name: kernel.event_listener, event: kernel.exception }

And in ExceptionListener.php i have:

class ExceptionListener
{
    public function __construct(Environment $engine) {
        $this->engine = $engine;
    }
    public function onKernelException(ExceptionEvent $event)
    {
        dd($event->getThrowable()->getMessage());
    }
}

Still nothing happens, the listener isn’t triggered.

Everything works as expected in dev environment, the problem presents itself only in prod.

Looking at a phpinfo(); page i see that the error reporting is enabled, so i think that the problem lies in the symfony configuration.

I’m using php 7.4 and have no real access to the php.ini file

How do I cache a page with file_get_contents?

I’m pretty new among you.

How do I cache a page with file_get_contents?

$site = file_get_contents('https://price.mysite.com/gold.php');

echo $site;

How can I print the output of this page to the cache file?

I want to show prices found here on another web page. Prices are updated every 30 minutes. On the page I will create, I want to use cache instead of constantly querying here. How can I do that?

Develop Webapplications for SAP S4 HANA without UI5 Framework [closed]

I’m looking for a smooth way to develop simple webapplications for SAP HANA without using UI5 Framework.

I know the common way to develop Fiori Apps is using either the Business Application Studio or Visual studio. Both require the SAPUI5 Framework, both consume OData Services and both are most likely based on CDS Views.

I want to avoid this “common” way because it requires a deeper knowledge of above mentioned technologies, which i currently don’t have.

My solution so far is using an external webserver, communicating via SOAP webservices and just point to that webserver when clicking the Tile in the Fiori Launchpad.

enter image description here

Is there an alternative to SAPUI5 for Fiori App Development, that does not require an external webserver?

php7.4 unable to read memcached data saved by pylibmc

I keep getting these errors trying to read memcached variables stored by python3 scripts runnnig pylibmc.

PHP Warning: Memcached::get(): could not decompress value: unrecognised compression type in xxx

I’m saving some test data using this python3 script:

#!/usr/bin/env python3
import json
import pylibmc

mc = pylibmc.Client(["127.0.0.1"], binary=True, 
     behaviors={"cas": True, "tcp_nodelay": True,"ketama": True})

mc.set('testvar', json.dumps('{"greeting": "Hello", "title": "Mr"}'), 3600)

Using telnet to fetch the variable from memcached shows the data is NOT compressed, but saved in clear text:

ubuntu@server:~/bin$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get testvar
VALUE testvar 16 81
"{"greeting": "Hello", "title": "Mr"}"
END
^]
telnet> quit
Connection closed.

I have set the compression limit in the php-memcached settings to a huge number:

/etc/php/7.4/cli/conf.d/25-memcached.ini contains:

extension=memcached.so
; You need to install php-igbinary package to use igbinary serializer
; and php-msgpack to use msgpack serializer
memcached.serializer=php
memcached.compression_threshold=9999999999

The PHP script trying to read the data:

<?php
    echo ini_get("memcached.compression_threshold") . "n";
    $mc = new Memcached();
    $mc->addServer('localhost', 11211);

    $data = json_decode($mc->get('testvar'));
    echo $data;
    echo "n";

?>

and the output:

9999999999
PHP Warning:  Memcached::get(): could not decompress value: unrecognised compression type in /home/ubuntu/bin/memcache_get.php on line 6

This is on a freshly installed Ubuntu 20 LTS system with python 3.8.10 and pylibmc Version: 1.6.1

Any hints anyone?

Issues with Posting System PHP

Now, I have a posting system. There’s an input field and a submit button. After entering something in the field and clicking the button, it gets displayed as a post.

Now, let’s say you make multiple posts (two or three), all of them should be the same (except for the body of the post). However, the issue I’m encountering is that the first post has a background-color of Silver (#C0C0C0), but the second or third posts don’t. However, the body, and other content in the post such as the like and comment buttons are there.

To fix this, I inspected the first post and it highlighted only the first post, not the second or third posts, when I hovered over the post div in the console. The posts should have a background color of silver, and if you have multiple posts, there should be some white space in between the posts. But if I increase the height of the background-color, it covers both the first and second post, without any white space. And when I did this (increased the height of the background-color) of the post and inspected the second post, it highlighted both the first and second post as <div class="textPost">, without any white space in between the two posts. I want there to be if, someone enters more than one post, the posts should have white space between them, and it should be like there’s multiple separate posts, and all the posts have a background-color of #C0C0C0 (Silver). How can I accomplish this? Please help.

Code:

<div class="textPost">
  <?php

  $sql = "SELECT * FROM posts";
  $result = mysqli_query($connection, $sql);
  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

  ?>
  <div class="textpostFormat">
    <-- All the post content (body, like button, comment button, etc.) -->
  </div>
  <?php

  }
}

  ?>
</div>

<style media="screen">
   
.textPost {
  margin-top: 170px;
  width: 650px;
  height: 400px;
  background-color: #C0C0C0;
  position: fixed;
  margin-left: 685px;
  border-radius: 15px;
}

.textpostFormat {
  margin-left: -640px;
  position: fixed;
}