Home > database >  PHP how to assign a session variable to 0 only once
PHP how to assign a session variable to 0 only once

Time:12-30

Is it possible to create a session variable that is assigned to 0 at the beginning and then it never returns back to this code. I want to create some kind of global iterator which is accessible in every php file. It increments every time photo is uploaded, but it must have some value at the beginning.

<?php
session_start();
$_SESSION['imageIterator'] = 0; // THIS SCRIPT IS EXECUTED EVERY TIME WHEN YOU UPLOAD FILES, SO EVERY TIME IT WILL BE ASSIGNED TO 0. I WANT IT ONLY ONCE
// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$title = $_POST['title'];
$author = $_POST['author'];
$watermark = $_POST['watermark'];
$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$_SESSION['title'] = $title;
$_SESSION['author'] = $author;
// START PHP Image Upload Error Handling --------------------------------------------------

if($fileSize > 1048576) { // if file size is larger than 1 Megabyte
    echo "ERROR: Your file was larger than 1 Megabytee in size.";
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    exit();
} 
// END PHP Image Upload Error Handling ----------------------------------------------------
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
    echo "ERROR: File has more than 1 megabyte";
    exit();
}
else {$_SESSION['imageIterator']  ;} // HERE THE ITERATOR!
// ---------- Include Universal Image Resizing Function --------
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "resized_uploads/resized_$fileName";
$wmax = 200;
$hmax = 125;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);

//include TextToImage class
require_once 'TextToImage.php';

//create img object
$img = new TextToImage;

//create image from text
$text = $watermark;
$img->createImage($text);

$img->saveAsPng("watermark_$fileName",'uploads_watermarks/');
// ----------- End Universal Image Resizing Function -----------
// ---------- Start Convert to JPG Function --------
if (strtolower($fileExt) != "jpg") {
    $target_file = "uploads/resized_$fileName";
    $new_jpg = "uploads/resized_".$kaboom[0].".jpg";
    ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
}
// ----------- End Convert to JPG Function -----------
// ---------- Start Image Watermark Function --------
$target_file = "uploads/".$kaboom[0].".jpg";
$wtrmrk_file = "uploads_watermarks/watermark_".$fileName.".png";
$new_file = "uploads_protected/protected_".$kaboom[0].".jpg";
ak_img_watermark($target_file, $wtrmrk_file, $new_file);
// ----------- End Image Watermark Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
?>

CodePudding user response:

Just check whether the session value already exists before you assign it...

session_start();
if (!isset($_SESSION['imageIterator']) $_SESSION['imageIterator'] = 0;

CodePudding user response:

i think this should work :

if ($moveResult != true) {
    echo "ERROR: File has more than 1 megabyte";
    exit();
}
else {
    if(!isset($_SESSION['imageIterator'])){
        $_SESSION['imageIterator'] = 1;
    }else{
        $_SESSION['imageIterator']  ;
    }
} // HERE THE ITERATOR!

**notice : remove this line : ** $_SESSION['imageIterator'] = 0; // THIS SCRIPT IS EXECUTED EVERY TIME WHEN YOU UPLOAD FILES, SO EVERY TIME IT WILL BE ASSIGNED TO 0. I WANT IT ONLY ONCE

  • Related