I am needing help. I want to create files with variable file names - I am creating a membership website and want each member to be able to have a blog username.htm The problem is that when I try to create this using the fopen function it is not recognizing the variable. This is the code I had:

$r=mysql_query("SELECT * FROM members WHERE Username='$_SESSION[username]'");
while($userinfo=mysql_fetch_array($r))
$blog=$userinfo[Username];
$cblog="$blog.htm";
$createblog=fopen($cblog,a);

Please can someone help. Thanks.

Dani AI

Generated

Short answer and safe path forward.

The immediate problem is almost always a syntax/quotation issue (as pointed out) plus using unsanitized user input as a filename. Array keys and mode strings must be quoted in PHP, and user-supplied text must never be fed directly to filesystem functions. Rather than creating one static HTML per user, consider 's suggestion: a single PHP template is simpler and search engines index dynamic pages fine when you expose stable, clean URLs and use canonical tags and a sitemap. Generate static files only for performance (or CDN) or if you must.

A practical, secure approach:

  • Validate and sanitize the username into a safe token (whitelist characters).
  • Build an absolute path inside a dedicated directory and ensure it exists.
  • Use safe writing functions with error checking and exclusive locks.
  • Prevent path traversal by reducing the name to a controlled set of characters and checking it's not empty.

Example pattern (conceptual):

$safe = preg_replace('/[^a-z0-9_-]/i', '', $username);
$dir  = __DIR__ . '/blogs/';
$path = $dir . $safe . '.html';
if (!is_dir($dir)) mkdir($dir, 0755, true);
file_put_contents($path, $htmlContent, LOCK_EX);

Extra cautions and SEO notes:

  • Ensure files are created with correct permissions (owner writable, others readable).
  • Check for collisions and handle duplicates (two users with same sanitized name).
  • If you prefer "static-looking" URLs without creating files, use rewrite rules to map friendly paths to the single template.
  • Keep content authoritative and unique per user; use rel=canonical and site maps to avoid duplicate-content penalties.

This keeps the code safe, predictable, and SEO-friendly while avoiding the brittleness of writing arbitrary files from raw session input.

Recommended Answers

All 4 Replies

I think you are going about this the wrong way. Why not take advantage of what php has to offer to simplify everthing. You can have one php page generate a different blog for each member without having to create seperate static html pages.

I think you are going about this the wrong way. Why not take advantage of what php has to offer to simplify everthing. You can have one php page generate a different blog for each member without having to create seperate static html pages.

I had thought of doing it this way initially but then I thought that perhaps it would be better for my members SEO if each one had a static blog. Would a static page not achieve better SEO results than a dynamic one?

I am needing help. I want to create files with variable file names - I am creating a membership website and want each member to be able to have a blog username.htm The problem is that when I try to create this using the fopen function it is not recognizing the variable. This is the code I had:

$r=mysql_query("SELECT * FROM members WHERE Username='$_SESSION[username]'");
while($userinfo=mysql_fetch_array($r))
$blog=$userinfo[Username];
$cblog="$blog.htm";
$createblog=fopen($cblog,a);

Please can someone help. Thanks.

if that is your code it doesn't work because you forgot two apostrophe in username.

$_SESSION
$userinfo

You can get the seo you are looking for by using mod_rewrite.

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.