Jump to content

[SOLVED] Using variables from another php page?


helraizer

Recommended Posts

Hi folks,

 

I have a php script that is:

 

<?php 
   $color[0] = imagecolorallocate($image, 0, 0, 0);
      $color[1] = imagecolorallocate($image, 255, 205, 50);
  $color[2] = imagecolorallocate($image, 0, 155, 0);
  $color[3] = imagecolorallocate($image, 255, 255, 0);
  $color[4] = imagecolorallocate($image, 255, 0, 0);
  $color[5] = imagecolorallocate($image, 0, 0, 255);
  $color[6] = imagecolorallocate($image, 255, 0, 255);

$c = mt_rand(0,6);

imagettftext($image, $font_size, 1, $xi, $yi, $color[$c], $this->font, $code[0]) or die('Error in imagettftext function');

?>

 

Which works fine, $c does what $c should do, but is there any way for me to reference that same $c in another/a different php page? Like with include you can use functions defined in the php page, could you do the same with variables?

 

Thanks in advance,

 

Sam

Link to comment
Share on other sites

yeah. "include ('<your file>');" basicly has the effect of copying that file to where the "include..." is.

 

Meaning that all varibles, database connections etc in one work in the other (remember that php is parsed top to bottom!). This also allows you to place html etc in the other file (useful for headers and stuff).

Link to comment
Share on other sites

Thanks guys, this works now in theory, but there is a problem: the file that has $c defined is a file to create an image.

 

the code in the second php file is:

 

<?php 
if ($c == 0) { echo "Please enter the code you see in black: "; }
elseif ($c == 1) { echo "Please enter the code you see in gold: ";}
elseif ($c == 2) {echo "Please enter the code you see in green: "; } 
elseif ($c == 3) {echo "Please enter the code you see in turquoise: "; }
elseif ($c == 4) { echo "Please enter the code you see in red: "; } 
elseif ($c == 5) {echo "Please enter the code you see in blue: " ;} 
else {echo "Plesae enter the code you see in pink: ";} ?>

 

It always echo's "Please enter the code you see in black:".

Also,  when I include the file I get:

 

GIF87aÈPçÿÍ2›ÿÿÿÿÿÿXÿÿÿÝÜⲯ§ÿÍ2›ÿÿÎÍÈ___???ŸŸŸØ×Ó»¹²ßßßÝÜØÅýëëéõõôÔÓÎîíëööõ¿¿¿âáÞÃÁ»ßòߣµ››¬’ŸÙŸt­oo§hY¥S]Í¿æ¿_À_t§M#„#‡Àƒ, )F¦BÅØÂ` l …ª}©Ï¦.¢,Ÿ&€º}:‡Œ±…]ªYžB¢>×êִƯ¹Ì´SfS{Èz?´?a®]/V/£cc8¬7„“¹wžw¸ß·¢¢ŸÅšy²t7J7”Î’;œ;wÄu·Þ·›'a'7«60¤.¥Y¹X¥Ë¢'œ';;;ææãòòñééæÌÊÅoooåäâúúùÙØÔúúúááÞ···çççÛÛÛòñð÷÷öÃÃﯯøøøïïïôóòððîýýýÚÙÕùùøéè樦¡333000¤¢œ666***š˜“%%%...,,,žœ—---¢¡›+++—•'''222999555777111///¦¤Ÿ®¬§¤¢:::888444&&&AAA¹·²FFF¹·±¢ ›(((¡Ÿš§¦¡Ÿ˜)))¡ ›ž˜ žš™—’–”Ž«©£°¯«¥£°®©>>>«©¤•“Ž›š”‘Šœš•­«¦@@@===º¸³BBB¶´¯!!! ž™sqsOOOLLLHHH¿½¸©§¢¦¤ž˜–¥£ž›–£¡œ§¥ ˜–‘›•sqp¨§¢«ª¥¬«¦¦¥ ¤£žyxw£¢£¡¢ œŸž™¡Ÿ›¢¡¦¤ ©¨¤ª¨¤«©¥©§£Ÿ™ Ÿšyxz"""“‘Œ$$$´²­DDDJJJKKKMMMQQQPPP¾¼·´³®±°¬###±¯ª<<<·µ°›™”³±«CCC£¢žŒŠ…³±¬°¯©½»¶NNNÃÁ¼RRRœ˜,ÈPþi5…R Kp\xd>QœðE<”AÔ&,±ÀBHP¡DðákDic£Ff‘ƒÄÑ#gÎÞ»"Ò>¾Ó( iøyöYgv¦ ¢š/ŸY¦‚^áyòUˆNøD!ZiQ’F2sŠ=Ÿ¤Œ:«ÃÈKdvÌŒÐx „3Ó`ˆ'È& BÏ,ÈaD ›ÔÒ"ú‚šgza„ zX¥f”™VY‚¾8#`„¢™–<ãàÏø<×S!!"‹#JÒÌFU"Z!Ù"«`J›8’I&’ ÂØ"´$J)øTRÉ#…H<äLI/•Å‹)‡,Ät–Á-«¢H6·ØÃJ0쌰Š$þ‘`Ì*I‰¢ …¢e‰ jX¸¥˜Ö(lÇÜ ¯jÉGø$‚Ï_9$Š)‰TŽø}¶ $‰”SfoþÕÕˆ´8!"›´dÙk’«D1¢FõI‚ .Õ„Ë4ŸÐ""‹@ ÍmäÐWã|UÎ1¢Òi"׳ "¦ ϱ«$ïidÌ"|%ð´$ÌÅE·æXliÄžv¬ÅRYj6fPŸl^]` Kˆ" $<"yãtïJ¢Ö^•"œ™ˆ<¸ÔBËþ´bUsvJPÎ B />‡SŽÀõù‚E ² ‚ÅÈaK'·Bˆ<ò@B¬$'ã°´ «ØGÚH‹DQÈNgÃçk™¹É&¥‘ã9µEÕ¹ÊH£hNÏ+¯±ÂêêY±úrÌ„ÆÛ&~øuF¦=ÿ{ó§ý~ €MBÞ; Please enter the code you see in black:

 

Also, $c is within a function on the other page, could that be a problem?

 

Sam

Link to comment
Share on other sites

You cannot output an image directly in a web page (using include won't work for what you are doing.)

 

To do what you want, you need to use a session.

 

Start the session on the page where you set the variable.

 

Start/resume the session in the image code to access the value.

Link to comment
Share on other sites

<?php 
   $color[0] = imagecolorallocate($image, 0, 0, 0);
      $color[1] = imagecolorallocate($image, 255, 205, 50);
  $color[2] = imagecolorallocate($image, 0, 155, 0);
  $color[3] = imagecolorallocate($image, 255, 255, 0);
  $color[4] = imagecolorallocate($image, 255, 0, 0);
  $color[5] = imagecolorallocate($image, 0, 0, 255);
  $color[6] = imagecolorallocate($image, 255, 0, 255);

$c = mt_rand(0,6);

imagettftext($image, $font_size, 1, $xi, $yi, $color[$c], $this->font, $code[0]) or die('Error in imagettftext function');

?>

 

Hate to burst your bubble sam but I have an issue with that you don't define $image as an image resource, this should cause your script to scream at you.

 

You can place this in a function so long as the whole function develops the image or the image handler comes into it and goes out of it

Link to comment
Share on other sites

<?php 
   $color[0] = imagecolorallocate($image, 0, 0, 0);
      $color[1] = imagecolorallocate($image, 255, 205, 50);
  $color[2] = imagecolorallocate($image, 0, 155, 0);
  $color[3] = imagecolorallocate($image, 255, 255, 0);
  $color[4] = imagecolorallocate($image, 255, 0, 0);
  $color[5] = imagecolorallocate($image, 0, 0, 255);
  $color[6] = imagecolorallocate($image, 255, 0, 255);

$c = mt_rand(0,6);

imagettftext($image, $font_size, 1, $xi, $yi, $color[$c], $this->font, $code[0]) or die('Error in imagettftext function');

?>

 

Hate to burst your bubble sam but I have an issue with that you don't define $image as an image resource, this should cause your script to scream at you.

 

You can place this in a function so long as the whole function develops the image or the image handler comes into it and goes out of it

 

No worries, you didn't burst my bubble ;) What I included there wasn't the entire code. =) I didn't include it all, just isolated that part of it to show what I meant. It's a complicated image so the whole script is 181 lines.. (including empty lines) so didn't think it wise to post it all. I only used the <?php ?> tags for the syntax highlighting, too.

 

Sam

 

Edit: @PFM.. I will try the sessions idea. Thank you. =)

Link to comment
Share on other sites

H'okay.. so now I have a predicament.

 

PFM's session method worked. So now

 

<?php 
if ($_SESSION['c'] == 0) { echo "Please enter the code you see in black: "; }
elseif ($_SESSION['c'] == 1) { echo "Please enter the code you see in gold: ";}
elseif ($_SESSION['c'] == 2) {echo "Please enter the code you see in green: "; } 
elseif ($_SESSION['c']== 3) {echo "Please enter the code you see in turquoise: "; }
elseif ($_SESSION['c'] == 4) { echo "Please enter the code you see in red: "; } 
elseif ($_SESSION['c'] == 5) {echo "Please enter the code you see in blue: " ;} 
else {echo "Plesae enter the code you see in pink: ";} ?>

 

So that now works.. but!! the text is in a delay with the image.. xD  So if the image text is gold, on first load the text will say "Please enter the code you see in black", so if you refresh the page the image text will change colour, say, to red and the text will now say "Please enter the code you see in gold", refresh and the image text changes yet again and it reads "Please enter the code you see in red".

 

Anyway to get around this?

 

Sam

Link to comment
Share on other sites

when you set a session value on a page after the headers have been sent i.e

<?php
session_start();
echo "Hello World.";
$_SESSION['c'] = "Yes";
echo $_SESSION['c'];
?>

it will not pick up the new session value

you need to update sessions pre sending any header data, or use the variable used to declrae the session as the value i.e

<?php
session_start();
echo "Hello World.";
$var = "yes;
$_SESSION['c'] = $var;
echo $var;
?>

Make sense?

Link to comment
Share on other sites

Also, you need to play browser/server and step through what is going on -

 

Browser requests your main php page.

 

Server reads php file, invokes the php language engine to parse and execute code, code sends HTML output, including your <img src="..." tag, to the browser.

 

Browser sees <img src="..." tag and requests the url of the image.

 

Server reads php file, invokes the php language engine to parse and execute code, code sends image header and image data to browser.

 

Is your program logic setting the value and storing it in the session in the proper place?

Link to comment
Share on other sites

Argh, silly mistake on my behalf. Before, I set the session in the image code and called it in the main file, which caused the /delay/ so now I reversed that; set the session in the main page and called it in the image, and now it works.

 

Thanks guys (and perhaps Gals, who knows)!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.