Click to See Complete Forum and Search --> : header("x=1");
gert cuykens
10-01-2006, 06:08 PM
I was wondering how i can post variables around form one php file to a other with post ?
i was thingking something like this ?
....1.php.....
header("x=1");
header("Location: 2.php");
....2.php.....
x=$_POST['x'];
bokeh
10-01-2006, 06:15 PM
Headers have nothing whatsoever to do with the $_POST array.
gert cuykens
10-01-2006, 06:46 PM
i think i need to use $_SESSION["x"]=1 :D
NogDog
10-01-2006, 07:10 PM
i think i need to use $_SESSION["x"]=1 :D
For PHP files on the same server and domain, sessions are probably the best way to go (and that's what they're made for).
pcthug
10-01-2006, 07:18 PM
Try using this function:
/*
$host - Just the hostname. No http:// or /path/to/file.html portions
$path - The /path/to/file.html part
$data - The query string, without initial question mark
$method - GET or POST, case-insensitive, if left blank POST will be used (optional)
$useragent - If true, 'MSIE' will be sent as the User-Agent (optional)
*/
function sendToHost($host, $path, $data, $method = 'GET', $useragent = true)
{
$method = strtoupper($method);
if($method != 'POST')
$method = 'GET';
$fp = fsockopen($host, 80);
if($method == 'GET')
$path .= '?' . $data;
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
if($method == 'POST')
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if($useragent)
fputs($fp, "User-Agent: MSIE\r\n");
fputs($fp, "Connection: close\r\n\r\n");
if($method == 'POST')
fputs($fp, $data);
while(!feof($fp))
$buf .= fgets($fp,128);
fclose($fp);
return $buf;
}
Here are some examples:
sendToHost('www.google.com', '/search', 'q=My+Query');
sendToHost('www.yourdomain.com', '/2.php', `x=1', 'POST', 'Firefox 1.5');
gert cuykens
10-01-2006, 07:55 PM
thats a nice script :D wondering how it performs against sessions ?
ps why doesnt this work ?
EDIT:: forgot session_start(); DOH!!
<?//1.php
$_SESSION["data"]=1;
header("Location: 2.php");
?>
<?//2.php
echo $_SESSION["data"];
?>
pcthug
10-01-2006, 08:04 PM
Try affixing session_start() (http://www.php.net/session_start) to the start of your scripts, like so:
<?//1.php
session_start();
$_SESSION["data"]=1;
header("Location: 2.php");
?>
<?//2.php
session_start();
echo $_SESSION["data"];
?> Without it, the SESSION superglobal variable array is useless.
gert cuykens
10-01-2006, 08:07 PM
What about performance and security doing your script as localhost instead of sessions?
pcthug
10-01-2006, 08:18 PM
Well, if you can implement session instead of post data I would. If you can't implement sessions on the form page I would suggest either using my script or implememting the GET variable array.
gert cuykens
10-01-2006, 08:20 PM
Why doesnt this work ?
<?
session_start();
$_SESSION["img"]=readfile('test.jpg');
echo '<img src=./img.php>';
?><?
session_start();
header('Content-type: image/jpeg');
echo $_SESSION["img"];
?>
pcthug
10-01-2006, 08:56 PM
Why not just pass the filename through sessions rather then raw image data?
<?
session_start();
$_SESSION["img"]= dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test.jpg';
echo '<img src=./img.php>';
?>
<?
session_start();
header('Content-type: image/jpeg');
readfile($_SESSION["img"]);
?>
NogDog
10-01-2006, 09:01 PM
Try just saving the filename in the session variable, then in the img.php file do the readfile() on that value:
<?php
session_start();
header('Content-type: image/jpeg');
if(isset($_SESSION['img']))
{
$image = basename($_SESSION['img']); // make sure only show files in this directory
if(file_exists($image))
{
readfile($image);
}
else
{
header('HTTP/1.1 404 Not Found');
}
}
?>
pcthug
10-01-2006, 09:04 PM
Or you could...
<?
session_start();
$_SESSION["img"] = base64_encode(file_get_contents('test.jpg'));
?>
<?
session_start();
$data = base64_decode($_SESSION["img"]);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/jpeg');
imagejpeg($im);
}
?>
gert cuykens
10-01-2006, 09:50 PM
Actualy the image file needs to be replaced with a xmlHttp post but when it didnt work i changed the xmlHttp post with a image file so i could find out if it was php sql or the xmlHttp post that didnt play along. To make a long story short, server send sql data, data gets displayed and now i am making a xmlHttp button that sends a part of the recieved data back to the server to convert it to a image because javascript cant do it.
So xmlHttp.send(image) to the first php file that generates html that includes the second php file trough <img src=2.php> that recieves the data from sessions.
The main goal is to get the image survive trough all the converting from php sql js collations with as little overhead as possible :D
gert cuykens
10-01-2006, 10:09 PM
is base64 the best way to ship bin files ?
gert cuykens
10-02-2006, 03:49 PM
$mime=mime_content_type($data);
if($mime=='image/jpeg')
{
header('Content-Type: image/jpeg');
echo imagejpeg($im);
} mime_content_type doest like $data nor $im nor imagejpeg($im) ?
How do i get the mime type so i can generate diferent images ?
pcthug
10-02-2006, 06:28 PM
Try this:
$mime= exec(trim('file -bi ' . escapeshellarg($data)));
if($mime=='image/jpeg')
{
header('Content-Type: image/jpeg');
echo imagejpeg($im);
}
gert cuykens
10-03-2006, 11:43 AM
ERROR: cannot open `ÿØÿà'
is there a shell command that can take base64 or a raw string as a argument or with < ?
i think this works but i dont now for sure exec(trim('file -bi < '.$data));
Nope it does not :D