I'm using sessions to receive my info from my database table... But I'm not quite sure how to get a image because if I just say this:

echo "<img src='" . $_SESSION['pic_location'] . "' />";

I only get like a little black dot thingy.

I don't know if one could use the img tag in a certain way or not...

it gives me a string of the image's path and not the image itself. How can I solve this matter?

Thank you!

:)

Dani AI

Generated

— that tiny black dot usually means the browser could not fetch a real image file at the src you sent it. Common causes: the session value contains a server filesystem path (for example a Windows path or a path outside the web root), the path has backslashes or spaces that break the URL, the session value is empty because session_start() wasn't called, or the webserver returns 404/403. was right to check the generated HTML; also use the browser DevTools Network panel and paste the src URL into the address bar to see the real error.

A safe way to output a web-accessible path (and avoid XSS) is to ensure the stored value is a URL or a path relative to the web root, then escape it before printing:

<?php
session_start();
$src = isset($_SESSION['image_path']) ? $_SESSION['image_path'] : '/images/default.png';
$src = htmlspecialchars($src, ENT_QUOTES, 'UTF-8');
?>
<img src="<?php echo $src; ?>" alt="profile photo">

If your DB stores filesystem paths or blobs, serve images through a script so the browser gets proper headers and never sees server paths. Example pattern:

<?php
// image.php?id=123
// fetch $filePath from DB
if (!is_readable($filePath)) { http_response_code(404); exit; }
$type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filePath);
header('Content-Type: '.$type);
readfile($filePath);
exit;
?>

Quick checklist:

  • Paste the src value into a browser to confirm the image loads.
  • Use forward slashes and URL-encode spaces (rawurlencode on filenames).
  • Confirm session_start() runs before reading the session.
  • Check file permissions and webserver alias/config if files are under a different folder.
  • If using CSS, confirm no styles force the image to 1x1 pixels.

These steps will reveal whether the problem is a bad URL, permissions, or an output/escaping issue.

Member Avatar for Member #120589

why are you using sessions? you should be able to pull them without using sessions. Anyway, look at 'view source' in the browser to see the actual html.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.