473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

assigning $_SESSION

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.

10 2864
Yes use sessions, you need to start the session each time your going to use it.

session_start( )
Mar 25 '10 #2
Markus
6,050 Expert 4TB
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
xyrhou
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
6,050 Expert 4TB
Yes .
Mar 25 '10 #5
xyrhou
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
6,050 Expert 4TB
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
xyrhou
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
xyrhou
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
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
xyrhou
14
Thank you so much it works now.. ^_^
Mar 25 '10 #11

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

Similar topics

0
by: Phil Powell | last post by:
What is the most standardized method of utilizing the CURL functions in PHP (version 4.3.2) to be able to retrieve the contents of a remote URL that happens to be dependent upon $_SESSION for its...
2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
5
by: h | last post by:
I am at my wits end; I can not see A SINGLE THING I'm doing wrong. I've read every fricking session related doc. I've searched every fricking group. NO ONE seems to have had the problem I'm having...
21
by: axlq | last post by:
Someone please tell me if I've discovered a PHP bug. I'm sitting in front of several computers on my home network, behind a NAT firewall/router. I am testing my web site on these different...
12
by: Michael Windsor | last post by:
I've been trying to integrate some PHP pages of my own with some existing code. The details of this are for the support forums for that code (where I have been asking questions), but I wonder if...
2
by: somecrazyguy | last post by:
Take the following code, one would think that there was absolutely no link between $test and $_SESSION. But if you reload the page, guess what... "After=FAILED". Why? Because for some reason,...
4
by: dpinion | last post by:
Greetings, I am trying to do some simple session stuff. However it does not seem as though the session variable is being created for my site. I am running the latest version of PHP and apache that...
4
by: Pseudonyme | last post by:
Dear Sirs and Madams, Receive as information that storing a MYSQL result under $_SESSION was accelerating web page displays processes ? Absolutly needed but impossible to get this working ! I...
2
by: sharonDonnelly | last post by:
Hi Really dumb problem that's got me beat. Can someone help. The prolem: I'm trying to count the number of times an item has been clicked. There are many items. I want to create a session...
3
by: JRough | last post by:
I want to save two variables in a $_SESSION for use in another page: $_SESSION = $mark; $_SESSION = $num; then on the other page I did this to get the value: $mark =$_SESSION; $num =...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.