By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,701 Members | 1,782 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,701 IT Pros & Developers. It's quick & easy.

assigning $_SESSION

P: 14
Is it possible to call the $random in another page? i used the $random as a filename in the image that was converted from movie to jpeg.
Expand|Select|Wrap|Line Numbers
  1.  
  2. $random = rand(1000000, 9999999); 
  3. $_SESSION['random']=$random;
  4.  
  5.  
im planning to use the $random from page1.php in page2.php in these code to filter the url that im planning to display.
Expand|Select|Wrap|Line Numbers
  1. $sql = mysql_query("SELECT * FROM image WHERE url = '" . $random. "'") or die(mysql_error());    
  2.  
my problem is i cant pass the $random from page1.php to page2.php..

Please help im a newbie to php.
Mar 25 '10 #1

✓ answered by philipwayne

The headers already sent error just means output has already been sent to the browser no header modifications can happen. Either use output buffering or make sure to open the session before outputting anything.

Also do note that any whitespace is still a character which means it is still output make sure there are no newlines/tabs/spaces in front of the opening PHP tag.

Share this Question
Share on Google+
10 Replies


P: 50
Yes use sessions, you need to start the session each time your going to use it.

session_start( )
Mar 25 '10 #2

Markus
Expert 5K+
P: 6,050
A quick sessions example:

user.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Start the session before you do anything else!
  4. session_start();
  5.  
  6. // Use the $_SESSION super-global array to store data in session
  7. $_SESSION['username'] = "Markus";
  8. $_SESSION['userage'] = 19;
  9. ?>
  10.  
  11. <a href="hello.php">Go to hello.php</a>
  12.  
hello.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Remember to start the session!
  4. session_start();
  5.  
  6. // Any data you stored can be accessed via the same super-global
  7. printf('Your name is %s, and you are %d years old!',
  8.     $_SESSION['username'],
  9.     $_SESSION['userage']
  10. );
  11.  
Mar 25 '10 #3

P: 14
Can you assign another variable to Session? Like this:
Expand|Select|Wrap|Line Numbers
  1. $random = rand (100 , 200)
  2. $_SESSION['random'] = "$random";
  3.  
So that when i start session again in another page its value is the last $random?
Mar 25 '10 #4

Markus
Expert 5K+
P: 6,050
Yes .
Mar 25 '10 #5

P: 14
Thanks you so much. What a great community..

Im going to try to apply it on my code.

If i have further questions am i going to post another? or post it in this thread?

Thank you again!!
Mar 25 '10 #6

Markus
Expert 5K+
P: 6,050
You're welcome :)

If you have more questions on this topic, then go ahead and post it in this thread. Otherwise, if the question is on another topic, make a new thread.

Mark.
Mar 25 '10 #7

P: 14
When i tried putting a session on my pixel.php i got these error:
i got a
Expand|Select|Wrap|Line Numbers
  1.     header("location: pic.php");
at the end of the pixel.php

Do the header affect the Session?

Here is the error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\WWW\Sneakers\files\pic.php:2) in C:\xampp\htdocs\WWW\Sneakers\files\pic.php on line 5

The pixel.php is the one responsible in converting a swf movie to an image then saving it with filename $random = rand(1111, 9999); to the database

the pic.php is the one responsible in calling the url of the image then previewing the image.


Do i need to post the exact code here?
Mar 25 '10 #8

P: 14
Here is the code for pic.php
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. session_start();
  5. include("connection.php");
  6.  
  7.     echo "Pageviews = ". $_SESSION['random'];    //checking if Session is passed
  8.  
  9.     $ImgID = $_SESSION['random'];
  10.             $filename= $random;
  11.             $path = 'images/' . $filename . '.jpg';
  12.         $sql = mysql_query("SELECT * FROM image WHERE ImgID = '" . $ImgID . "'") or die(mysql_error());    
  13.         $row = mysql_fetch_array($sql);
  14.  
  15.  
  16.  
  17.  
  18.             echo "<center>";
  19.             echo "<img src=view.php?image=". $row['url'] . "&amp;mode=resize&amp;size=350x500 />";
  20.             echo "<br/>";
  21.  
  22.  
  23.  
  24.         echo "<br/>";    
  25.  
  26. ?>
  27.  
Here is the pixel.php

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. session_start();
  4.     include('connection.php');
  5. error_reporting(0);
  6. /**
  7.  * Get the width and height of the destination image
  8.  * from the POST variables and convert them into
  9.  * integer values
  10.  */
  11. $w = (int)$_POST['width'];
  12. $h = (int)$_POST['height'];
  13.  
  14. // create the image with desired width and height
  15.  
  16. $img = imagecreatetruecolor($w, $h);
  17.  
  18. // now fill the image with blank color
  19. // do you remember i wont pass the 0xFFFFFF pixels 
  20. // from flash?
  21. imagefill($img, 0, 0, 0xFFFFFF);
  22.  
  23. $rows = 0;
  24. $cols = 0;
  25.  
  26. // now process every POST variable which
  27. // contains a pixel color
  28. for($rows = 0; $rows < $h; $rows++){
  29.     // convert the string into an array of n elements
  30.     $c_row = explode(",", $_POST['px' . $rows]);
  31.     for($cols = 0; $cols < $w; $cols++){
  32.         // get the single pixel color value
  33.         $value = $c_row[$cols];
  34.         // if value is not empty (empty values are the blank pixels)
  35.         if($value != ""){
  36.             // get the hexadecimal string (must be 6 chars length)
  37.             // so add the missing chars if needed
  38.             $hex = $value;
  39.             while(strlen($hex) < 6){
  40.                 $hex = "0" . $hex;
  41.             }
  42.             // convert value from HEX to RGB
  43.             $r = hexdec(substr($hex, 0, 2));
  44.             $g = hexdec(substr($hex, 2, 2));
  45.             $b = hexdec(substr($hex, 4, 2));
  46.             // allocate the new color
  47.             // N.B. teorically if a color was already allocated 
  48.             // we dont need to allocate another time
  49.             // but this is only an example
  50.             $test = imagecolorallocate($img, $r, $g, $b);
  51.             // and paste that color into the image
  52.             // at the correct position
  53.             imagesetpixel($img, $cols, $rows, $test);
  54.         }
  55.     }
  56. }
  57.  
  58. // print out the correct header to the browser
  59. //header("Content-type:image/jpeg");
  60. // display the image
  61. //imagejpeg($img, "", 90);
  62.  
  63. $random = rand(1000000, 9999999); 
  64. $_SESSION['random']=$random;
  65.  
  66. $username = $_SESSION['username'];
  67. $userid = $_SESSION['userid'];
  68. $ImgID = $_SESSION['ImgID'];
  69.  
  70.     $filename = $random; //filename setting
  71.     $path = 'images/' . $filename . '.jpg'; // url of the image
  72.  
  73.     imagejpeg($img,$path,90);
  74.  
  75.     $qInsertImage = mysql_query("INSERT INTO image(url,ImgTitle) VALUES('".$path."' , '".$filename."')") or die(mysql_error()); // save to database
  76.  
  77.  
  78.  
  79.  
  80.     header("location: pic.php");    
  81. ?>
  82.  
Mar 25 '10 #9

P: 50
The headers already sent error just means output has already been sent to the browser no header modifications can happen. Either use output buffering or make sure to open the session before outputting anything.

Also do note that any whitespace is still a character which means it is still output make sure there are no newlines/tabs/spaces in front of the opening PHP tag.
Mar 25 '10 #10

P: 14
Thank you so much it works now.. ^_^
Mar 25 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.