Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:36:15+00:00 2026-06-08T12:36:15+00:00

So in my HTML markup I have an image tag like this one: <img

  • 0

So in my HTML markup I have an image tag like this one:

<img src="image_generation.php" alt="template" id="image" />

And the ‘src’ attribute links to a PHP script that generates an image using a couple of variables defined there which are mostly randomly generated.

Now, what I want to be able to do is to access those random variables in the page which includes the image generation script. I suppose I could send cookies and access them after the image tag as they technically should be readily available to the including file. I don’t want to send too much information, just a couple (10-20) variables. Not sure if in that case sessions would be a better choice, as I would have to send several cookies. Sessions also pose a problem as the including script gets the old session and I would have to refresh the page to obtain the values of the previously generated image. I suppose I could also set up a DB and access the DB in the including script, but the variables are temporary and I would have to delete them and that seems like a lot of fuss to me.

The image generation script finishes with:

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

And nothing can be sent to the browser before the header call or else the image won’t be displayed. If I use cookies or sessions, the image_generation.php would have to send both the image and set the cookie(s)/session.

None of the options (cookies, sessions or DB) really convince me, as there are problems with each in this particular situation. Can you think of a way to solve this? Thanks.

MAJOR EDIT #1:
Including script gets session of previously generated image without refreshing / Setting cookie(s) and/or a session in included script before / after sending image without output buffering does not pose a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:36:17+00:00Added an answer on June 8, 2026 at 12:36 pm

    You can use a $_SESSION, but to make the session available in the same script that included the <img> tag (which would have executed before the image script), you would need to make AJAX calls via JavaScript. An AJAX handler that runs at window.onload should have access to the $_SESSION created by the image script, since the image should have fully loaded when it executes.

    Example PHP handler getsession.php:

    header('Content-type: application/json');
    // Simply part of the session into JSON
    // Obviously you would want to limit this to only the variables 
    // that should be sent back, so you don't expose the session on the client side!
    echo json_encode(array($_SESSION['var1'],$_SESSION['var2']));
    exit();
    

    Example AJAX call (using jQuery since it will be easy to get started with)

    // Variable to hold the object returned by PHP
    var imgPHPSession;
    $(window).load(function() {
      $.ajax({
        url: 'getsession.php',
        dataType: 'json',
        success: function(data) {
          imgPHPSession = data;
        }
      });
    });
    

    Update:

    It can be done entirely in PHP, but would require changing your design a bit such that the variables necessary to generate the image are created in $_SESSION by the main script. They are then available in $_SESSION to image_generation.php to be used as needed, but are already known to the main script.

    Update 2:

    Since the image vars contain info about how it was created, if the image is not too large, you can actually create it in the main script and store it to disk. The image_generation.php script can still be used as the <img src>, but its purpose would then be to retrieve the correct image from disk and serve it back to the browser and delete it from disk when no longer needed. The $_SESSION is then available to both the main and image scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML markup. <div class=caption> <img class='active' alt='My Caption' src='path/to/image' />
I currently have the following HTML markup: <td class=title> <img src=my-image.png class=thumbnail /> <span
I have html markup like this <button id=button1 onclick=alert='yup'></button> and I want to make
I have a string contaning some html markup, like this: var html = <div>
I have following HTML markup, <div id=subcontent_l> <p> <a href=/membership-packages/><img height=202 width=644 alt= src=http://74.52.72.231/wp-content/uploads/2010/06/banner1.jpg
I have the following HTML markup, <section> <img width=106 height=113 title=key-staff-tim alt=key-staff-tim class=attachment-post-thumbnail wp-post-image
Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
I have the following HTML markup:- <select name=List1 id=l1> <option>One</option> <option>Two</option> <option>Three</option> <option>Four</option> <option>Five</option>
Let's say I have a string contaning HTML markup with many img tags that
I have displayed one of sprite's image in span tag. The span is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.