Help me to store an array as string and then redirect to another page

I’m using an OCR API and I want the result( $response['ErrorMessage'] ) to be stored as a string in #_SESSION['string']=jason_encode($response['ErrorMessage']); so that, I can use the result laer and then redirect to another page header("location:somepage.php");. I tried everything I know but its not working.

<?php
if(isset($_POST['submit']) && isset($_FILES)) {
    require __DIR__ . '/vendor/autoload.php';
    $target_dir = "uploads/";
    $uploadOk = 1;
    $FileType = strtolower(pathinfo($_FILES["attachment"]["name"],PATHINFO_EXTENSION));
    $target_file = $target_dir . generateRandomString() .'.'.$FileType;
    // Check file size
    if ($_FILES["attachment"]["size"] > 5000000) {
        header('HTTP/1.0 403 Forbidden');
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    if($FileType != "png" && $FileType != "jpg") {
        header('HTTP/1.0 403 Forbidden');
        echo "Sorry, please upload a pdf file";
        $uploadOk = 0;
    }
    if ($uploadOk == 1) {
   
        if (move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file)) {
            uploadToApi($target_file);
        } else {
            header('HTTP/1.0 403 Forbidden');
            echo "Sorry, there was an error uploading your file.";
        }
    } 
} else {
    header('HTTP/1.0 403 Forbidden');
    echo "Sorry, please upload a pdf file";
}

function uploadToApi($target_file){
    require __DIR__ . '/vendor/autoload.php';
    $fileData = fopen($target_file, 'r');
    $client = new \GuzzleHttp\Client();
    try {
    $r = $client->request('POST', 'https://api.ocr.space/parse/image',[
        'headers' => ['apiKey' => '5b501395bb88957'],
        'multipart' => [
            [
                'name' => 'file',
                'contents' => $fileData
            ]
        ]
    ], ['file' => $fileData]);
    $response['ErrorMessage'] =  json_decode($r->getBody(),true);
    if($response['ErrorMessage'] == "") {
?>
<html>
    <head>
    <title>Result</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
        <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.min.js'></script>
    </head>
    <body>
        <div class="form-group container">
            <div class="col-md-6">
            <label for="exampleTextarea">Result</label>
            <textarea class="form-control" id="exampleTextarea" rows="30"  style="width:500Px;height:400px;color:red;background-color:yellowgreen;">
            <?php
                foreach($response['ParsedResults'] as $pareValue) {
                    echo $pareValue['ParsedText'];
                }
            ?></textarea>
                </div>
        </div>
    </body>
</html>
<?php
    } else {
        
        header('HTTP/1.0 400 Forbidden'); 
        $string= json_encode($response['ErrorMessage']);
        echo $string;
        
       
    }
    } catch(Exception $err) {
        header('HTTP/1.0 403 Forbidden');
        echo $err->getMessage();
    }
}

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
 
?>

Session variables are just variables. Seems too simple an answer, right?
Well, if you have an array such as $errors=array(“error1”, “error2”, “ect”);
You can just store that array in the session array. Simply like $_SESSION[“errors”]=$errors;
This actually stores the array in the session array under the name you select. I used “errors”.

Then, on the next page, the session variables is already an array therefore, you can access it that way.
Such as echo $_SESSION[“errors”][3]; would display the fourth error. You can also parse thru them
like this:
foreach($_SESSION[‘errors’] as $key=>$value) {
echo ‘The value of $_SESSION[’."’".$key."’".’] is ‘."’".$value."’".’ < br>’;
}
Should work depending on what data you are sending. If you need to still use the JASON_ENCODE,
you can still do that, but, use this format instead: $_SESSION[“errors”] .= jason_encode(etc);
This will add another array value in the errors and concatenate it to the previous ones.
Once you are done processing the errors on the second page and no longer need them, make sure
that you UNSET them to dump them.

is it JASON or JSON ? :wink:

Hee! Flying fingers… Nice catch…

Sponsor our Newsletter | Privacy Policy | Terms of Service