Hi,

I am trying to get a php variable into a header, I have managed it in another file but it not having it this time and I do not know why.

Here is my php code

<?php
header("Location: photo.php?albumid=".urlencode($albumid));
include_once 'title.php';

$result = mysql_query("SELECT id FROM user WHERE user='$user'");
$id = mysql_result($result, 0);

if (isset($_POST['album']))
{
    $album = sanitizeString($_POST['album']);
    $result = mysql_query("INSERT INTO album (id, user, name) VALUES (NULL, '$id', '$album')");
}
else
{
    echo "<br /><br />";
    echo "Error: Please enter an album name";
}

$sql = "SELECT id FROM album WHERE name='$album'";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$albumid = mysql_result($result, 0);

echo $albumid; // correctly displays the album's id

?>

When this page is run and the header redirects the user the url is shown as:

photo.php?albumid=

rather than for example:

photo.php?albumid=3

Hope someone can help me fix this.

Thanks.

It looks like you haven't set the albumid variable. If this is coming in as a query string use $_GET.

Old versions of PHP used to set variables like this (a feature called register_globals which was really more of a major security flaw) but it was never considered good practice and thankfully deprecated.

I see, I am properly setting a value to the variable $albumid as I have tested it with the echo on line 23.

The variable is being set to the integer id value returned from the mysql_query on line 19.

I tried doing:

header("Location: photo.php?albumid=".urlencode($_GET['albumid']));

and

header("Location: photo.php?albumid=".urlencode($_GET['$albumid']));

I have also tried

header("Location: photo.php?albumid=".urlencode($albumid));

and

header("Location: photo.php?albumid=$albumid");

but none of these seem to work.

Thanks for quick reply.

I can see that you're echoing it on line 23, but you're not setting it until line 21 and printing it at line 2. Your location header should actually be redirecting the page at that point before the rest of the script runs and the variable is set I think. If you move line2 to line 24 you should find it works assuming that you haven't printed anything to the screen beforehand.

Right, but my problem with moving the header down is that the included file 'title.php' provides me with the variable $user on line 5 and also echoes out a bunch of stuff meaning that I can't put the header below the include_once statement.

That header if working correctly should cause an immediate redirect via "Location" so the rest of the script won't actually run anyway?

The rest of the code runs, I thought that the page would always run first and then run the header. The code saves an image album to a database which stores an auto_incremented ID, the user that created the album and the name of the album. That all runs and works correctly as I have checked in the database, which is why I am confused that it is not getting my $albumid variable

The header should do a redirect regardless of where it is, Im surprised you can see anything from this script. It doesn't surprise me that it runs so much as the header only redirects the browser, it doesn't stop the script running afterwards.

You say that line 23 "echo albumid" is echoing it back correctly, but not when the header is in place surely??!

No if the header is in place then you see photo.php which is a php page containing a html form which is used to upload images.

If I comment out the header then on screen is echoed the albumid when the form is submitted. echoing out the albumid is not important, that is just a check to make sure that it is actually getting the value.

Anyway, try this - 3 new lines of code entered:

ob_start() - start output buffering - output not sent to the browser
ob_end_flush() - flush the output to the browser and stop output buffering
ob_end_clean() - remove all output from the buffer so far and stop output buffering

This should mean your $user variable is still set.

<?php
ob_start();
include_once 'title.php';

$result = mysql_query("SELECT id FROM user WHERE user='$user'");
$id = mysql_result($result, 0);
 
if (isset($_POST['album']))
{
    $album = sanitizeString($_POST['album']);
    $result = mysql_query("INSERT INTO album (id, user, name) VALUES (NULL, '$id', '$album')");
}
else
{
    ob_end_flush();
    echo "<br /><br />";
    echo "Error: Please enter an album name";
}
ob_end_clean();
$sql = "SELECT id FROM album WHERE name='$album'";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$albumid = mysql_result($result, 0);
header("Location: photo.php?albumid=".urlencode($albumid));
echo $albumid; // correctly displays the album's id (wont display as the header redirects first)
 
?>

Yes that work thank you very much for all of your help.

I have not seen the ob_start() etc functions before, so they control output?

thanks again

Yes, ob stands for output buffering. Essentially the output is sent to a buffer rather than to the browser and is stored for later use. ob_start() turns output buffering on and you wont see any output before you call either ob_end_clean() which cleans the buffer (doesnt print anything that has been stored and stops buffering) or ob_end_flush() which flushes all the stored output to the browser at that point.

Anything you print after one of these two calls will print as normal. It's pretty useful for times when you have code like yours above.

That said, possibly a better option would be to split your title include into 2 or more scripts. One to do the printing of the page headers, titles and stuff and one to load required variables, then you can just include the separate bits as you need them. If you end up writing big applications you'll be lost without a very organised approach like that.

Yeah splitting it up sounds like a good idea to me, thanks again for all your help im sure ob_start() and the rest of em will come in handy in the future.

hi all, can anyone help me to resolve this issue.
I have this header scrip
$globe_id=$_GET['globe_id'];
header("location: globe_welcome.php?globe_id=".urlencode($globe_id));
It is not working properly.
When i am posting data through form is is not showing id in address bar.
Its result come like
http://mysite/more/globe_welcome.php?globe_id=
where i am wrong? can any body help me?

Member Avatar for LastMitch

@raovinesh

Can you started a new thread!

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.