assigning $_SESSION

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xyrhou
    New Member
    • Mar 2010
    • 14

    assigning $_SESSION

    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.
    Code:
    $random = rand(1000000, 9999999); 
    $_SESSION['random']=$random;
    im planning to use the $random from page1.php in page2.php in these code to filter the url that im planning to display.
    Code:
    $sql = mysql_query("SELECT * FROM image WHERE url = '" . $random. "'") or die(mysql_error());
    my problem is i cant pass the $random from page1.php to page2.php..

    Please help im a newbie to php.
  • philipwayne
    New Member
    • Mar 2010
    • 50

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

    session_start( )

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      A quick sessions example:

      user.php
      Code:
      <?php
      
      // Start the session before you do anything else!
      session_start();
      
      // Use the $_SESSION super-global array to store data in session
      $_SESSION['username'] = "Markus";
      $_SESSION['userage'] = 19;
      ?>
      
      <a href="hello.php">Go to hello.php</a>
      hello.php
      Code:
      <?php
      
      // Remember to start the session!
      session_start();
      
      // Any data you stored can be accessed via the same super-global
      printf('Your name is %s, and you are %d years old!',
          $_SESSION['username'],
          $_SESSION['userage']
      );

      Comment

      • xyrhou
        New Member
        • Mar 2010
        • 14

        #4
        Can you assign another variable to Session? Like this:
        Code:
        $random = rand (100 , 200)
        $_SESSION['random'] = "$random";
        So that when i start session again in another page its value is the last $random?

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Yes .

          Comment

          • xyrhou
            New Member
            • Mar 2010
            • 14

            #6
            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!!

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              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.

              Comment

              • xyrhou
                New Member
                • Mar 2010
                • 14

                #8
                When i tried putting a session on my pixel.php i got these error:
                i got a
                Code:
                	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.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs \WWW\Sneakers\f iles\pic.php:2) in C:\xampp\htdocs \WWW\Sneakers\f iles\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?

                Comment

                • xyrhou
                  New Member
                  • Mar 2010
                  • 14

                  #9
                  Here is the code for pic.php
                  Code:
                  <?php
                  	
                  session_start();
                  include("connection.php");
                  
                  	echo "Pageviews = ". $_SESSION['random'];	//checking if Session is passed
                  
                  	$ImgID = $_SESSION['random'];
                  			$filename= $random;
                  			$path = 'images/' . $filename . '.jpg';
                  		$sql = mysql_query("SELECT * FROM image WHERE ImgID = '" . $ImgID . "'") or die(mysql_error());	
                  		$row = mysql_fetch_array($sql);
                  	
                  	
                  		
                  		
                  			echo "<center>";
                  			echo "<img src=view.php?image=". $row['url'] . "&amp;mode=resize&amp;size=350x500 />";
                  			echo "<br/>";
                  		
                  			
                  		
                  		echo "<br/>";	
                  
                  ?>
                  Here is the pixel.php

                  Code:
                  <?php
                  session_start();
                  	include('connection.php');
                  error_reporting(0);
                  /**
                   * Get the width and height of the destination image
                   * from the POST variables and convert them into
                   * integer values
                   */
                  $w = (int)$_POST['width'];
                  $h = (int)$_POST['height'];
                  
                  // create the image with desired width and height
                  
                  $img = imagecreatetruecolor($w, $h);
                  
                  // now fill the image with blank color
                  // do you remember i wont pass the 0xFFFFFF pixels 
                  // from flash?
                  imagefill($img, 0, 0, 0xFFFFFF);
                  
                  $rows = 0;
                  $cols = 0;
                  
                  // now process every POST variable which
                  // contains a pixel color
                  for($rows = 0; $rows < $h; $rows++){
                  	// convert the string into an array of n elements
                  	$c_row = explode(",", $_POST['px' . $rows]);
                  	for($cols = 0; $cols < $w; $cols++){
                  		// get the single pixel color value
                  		$value = $c_row[$cols];
                  		// if value is not empty (empty values are the blank pixels)
                  		if($value != ""){
                  			// get the hexadecimal string (must be 6 chars length)
                  			// so add the missing chars if needed
                  			$hex = $value;
                  			while(strlen($hex) < 6){
                  				$hex = "0" . $hex;
                  			}
                  			// convert value from HEX to RGB
                  			$r = hexdec(substr($hex, 0, 2));
                  			$g = hexdec(substr($hex, 2, 2));
                  			$b = hexdec(substr($hex, 4, 2));
                  			// allocate the new color
                  			// N.B. teorically if a color was already allocated 
                  			// we dont need to allocate another time
                  			// but this is only an example
                  			$test = imagecolorallocate($img, $r, $g, $b);
                  			// and paste that color into the image
                  			// at the correct position
                  			imagesetpixel($img, $cols, $rows, $test);
                  		}
                  	}
                  }
                  
                  // print out the correct header to the browser
                  //header("Content-type:image/jpeg");
                  // display the image
                  //imagejpeg($img, "", 90);
                  
                  $random = rand(1000000, 9999999); 
                  $_SESSION['random']=$random;
                  
                  $username = $_SESSION['username'];
                  $userid = $_SESSION['userid'];
                  $ImgID = $_SESSION['ImgID'];
                  
                  	$filename = $random; //filename setting
                  	$path = 'images/' . $filename . '.jpg'; // url of the image
                  	
                  	imagejpeg($img,$path,90);
                  
                  	$qInsertImage = mysql_query("INSERT INTO image(url,ImgTitle) VALUES('".$path."' , '".$filename."')") or die(mysql_error()); // save to database
                  
                  
                  
                  	
                  	header("location: pic.php");	
                  ?>

                  Comment

                  • philipwayne
                    New Member
                    • Mar 2010
                    • 50

                    #10
                    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.

                    Comment

                    • xyrhou
                      New Member
                      • Mar 2010
                      • 14

                      #11
                      Thank you so much it works now.. ^_^

                      Comment

                      Working...