Hi all, can someone tell me why I am unable to concatinate the variable $id to the members.php ?

Its driving me mad. . .

$id = $row["id"];

        	        $_SESSION['SESS_MEMBER_ID'] = $id;		   
		   	setcookie("id", $id, time()+86400);
							
			$securecode = $row["securecode"];
			$_SESSION['SESS_SECURE_CODE'] = $securecode;
			setcookie("securecode", $securecode, time()+86400);
			
			$creff = $row["creff"];
			$_SESSION['SESS_CREFF'] = $creff;
			setcookie("creff", $creff, time()+86400);
			
		/*
		Login and go to members area											
		*/
		//header("location: members.php?id=" ' .$_SESSION['id']. ');
		//header("Location: members.php?id=$id");
		//  
		//header("Location: members.php?id= ' .$_SESSION['SESS_MEMBER_ID']. '")
		//securecode.php?id=
		//header("Location: members.php?id='.id.'");
		//header("Location: members.php?id=$id");
		header("Location: members.php?id=<? print '.$id ?>");

Dani AI

Generated

Quick checklist and practical tips to get that redirect working.

As described, the usual causes are not PHP’s concatenation syntax but the environment around the redirect. As noted, you can interpolate or concatenate variables in strings — that’s fine — but the real problems are: the session/cookie/variable state or output already sent to the browser. Verify these in order.

  1. Verify the value and the exact variable you intend to use before redirecting. Temporarily log or dump the ID so you know it isn’t empty or a different key than the one you set earlier. A common slip is setting one session key and trying to use a different one later.
  2. Redirects and cookies must be sent before any output. Check for stray whitespace, included files that send output, or a UTF-8 BOM. Use a headers-check helper (the built-in headers_sent()), or enable full error reporting to catch a “headers already sent” warning.
  3. Don’t try to put PHP open/close tags inside a quoted string — that will be sent literally, not executed. Build the URL from the variable (or read it from the session) and then issue the redirect. After sending the redirect header, stop execution with exit to avoid accidental further output.
  4. Security and robustness: URL-encode values you put into a query string, avoid putting sensitive tokens in GET parameters, and prefer server-side session state for secure data. When setting cookies, set path and secure/HttpOnly flags where appropriate.

If it still fails, replace the redirect temporarily with an echoed URL to see what would have been sent, check the browser Network panel for the actual HTTP response, and reduce to a minimal reproducible script so you can isolate the problem.

Recommended Answers

All 2 Replies

header("Location: members.php?id={$id}");
//or
header("Location: members.php?id=".$id);

header is a php command and is already inside php tags so there is no need to use php tags again, it's like playing with a string such as echo "members.php?id=$myvar";

String help:

strings in php are opened with a single or double quote, once opened they need to be ended with the same type quote and can be concatenated with a .

eg.

echo "hello ".$name."!";
echo 'hello '.$name."!";
echo 'hello '.$name.'!';
echo "hello {$name}!";
//all output
//hello Biiim!

note single quotes do not get special processing:

echo 'hello {$name}!\r\n';
//will print out exactly
//hello {$name}!\r\n

where as

echo "hello {$name}!\r\n";
//will print out hello Biiim!
// <- newline

you can escape characters in a string using a backslash eg.

echo "hello \"{$name}\"!";
//will print out 
//hello "Biiim"!

Infact your second header commented out should of worked:

//header("Location: members.php?id=$id");

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.