Session not working - where have I gone wrong?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Session not working - where have I gone wrong?

Post by genix2011 »

Hi,

Ok looked over it, and rewrote some parts, and tested it, so that the post vars are really saved in the session:

Code: Select all

<?php
/**
 * Requires PHP 5.3
 * Retrieves the value from $_POST or $_SESSION at index $name
 *   if found in $_POST and $set_session is true sets $_SESSION value at index $name to $_POST value
 */   
function get_safe_value($name, $set_session=true, $filter=null){
    $out = null;
    if(isset($_POST[$name])){
        if($set_session){
            $_SESSION[$name] = $_POST[$name];
        }
        $out = $_POST[$name];
    } else {
        if(isset($_SESSION[$name])){
            $out = $_SESSION[$name];
        }
    }
    
    if($out != null){
        if($filter != null){
            $out = $filter($out);
        } else {
            $out = str_replace("'", '', $out);
        }
    }
    
    return $out;
}

$catname = get_safe_value('catname');
$category = get_safe_value('category', false);
$catid = get_safe_value('catid');
$subid = get_safe_value('subid');
$subname = get_safe_value('subname');
$description = get_safe_value('description');
$video = get_safe_value('video');

$filter_func = function($val){
    return sprintf('%0.2f', preg_replace('/[^0-9.]/', '', $val));
};

$price = get_safe_value('price', true, $filter_func);
$postage = get_safe_value('postage', true, $filter_func);
$postage_location = get_safe_value('postage_location');
$photo = get_safe_value('photo');
$pic = (isset($_FILES['photo']['name']) ? $_FILES['photo']['name'] : null);
$p = (isset($_GET['p']) ? $_GET['p'] : null);

include "dbconn.php";
$cookietype = null;
$cookieid = null;

if(isset($_COOKIE['type'])){
    $cookietype = $_COOKIE['type'];
}

if(isset($_COOKIE['userid'])){
    $cookieid = $_COOKIE['userid'];
}
?>
if it still does not work, try using print_r($_SESSION) to see what is in the session.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

I don't really understand the top part, and I don't understand the $filter_func at all.
Why does the $title not work tho? What in my code is causing it, when $video is the same and does work.

Something must be causing it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Session not working - where have I gone wrong?

Post by genix2011 »

Hi,

forgot to add $title to the code:

Code: Select all

$video = get_safe_value('title');
The top part does the same all your ifs before did, but I outsourced it into a function.
It also checks if $_POST or $_SESSION for a specific key is set, if yes returns $_POST[key] or $_SESSION[key].
If the key was found in $_POST it also sets the $_SESSION value for this key to $_POST[key]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

You mean..

Code: Select all

$title= get_safe_value('title');
Thank you for this, I will have a go.
Still do not understand why some are working for me, and some are not.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Session not working - where have I gone wrong?

Post by genix2011 »

Yes.. :)

I don't know, but if you follow the simple concept, to first check for the existence of an global var and then use it, you'll be sure that this is going to work.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

Ok, and update.
I found out the reason it wasn't taking '$title', it thought there was a picture being uploaded and so was going down to the foot of the page script.
That had some other 'stripping out' code.

So I have removed that and it now INSERTS.
However, it still thinks there is a $photo uploaded, even if I leave the film blank.
And I am asking in my code if there is something in $photo.

So the saga goes on, but am a stage further.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

Right then, I am a cat's whisker away from getting this working.

All variables are now working, except for the file uploaded.

Code: Select all

if(isset($_FILES['photo']))
{
    $photo = $_FILES['photo'];
    $_SESSION['photo']=$photo;
} else { $photo=$_SESSION['photo'];}
This is storing the file details (I hope).

Then further down using an include file, I have this:

Code: Select all

<?php
if(get_magic_quotes_gpc()) {
$pic=($_FILES['photo']['name']);
      $input = array(&$_GET, &$_POST, &$_COOKIE, &$_ENV, &$_SERVER);
     
      while(list($k, $v) = each($input)) {
        foreach($v as $key => $val) {
          if(!is_array($val)) {
            $input[$k][$key] = stripslashes($val);
            continue;
          }
          $input[] =& $input[$k][$key];
        }
      }
      unset($input);
    }
    
  error_reporting(0);

  $change="";
  $abc="";

  define ("MAX_SIZE","400");
  function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
  }

  $errors=0;
  
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
    $image =$_FILES["photo"]["name"];
    $uploadedfile = $_FILES['photo']['tmp_name'];    
 
    if ($image) 
    {
 	
      $filename = stripslashes($_FILES['photo']['name']);
 	
  		$extension = getExtension($_FILES['photo']['name']);
      $extension = strtolower($extension);
			
  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		
 			$change='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors=1;
 		}
 		else
 		{

  $sizechange=filesize($_FILES['photo']['tmp_name']);


  if ($sizechange > MAX_*1024)
  {
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
  }


  if($extension=="jpg" || $extension=="jpeg" )
  {
  $uploadedfile = $_FILES['photo']['tmp_name'];
  $src = imagecreatefromjpeg($uploadedfile);
  }
  else if($extension=="png")
  {
  $uploadedfile = $_FILES['photo']['tmp_name'];
  $src = imagecreatefrompng($uploadedfile);
  }
  else 
  {
  $src = imagecreatefromgif($uploadedfile);
  }

  echo $scr;
  list($width,$height)=getimagesize($uploadedfile);
  $tmp=imagecreatetruecolor($width,$height);
  $newwidth1=142;
  $newheight1=($height/$width)*$newwidth1;
  $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
  
  imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$width,$height);
  imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
  $pic=($_FILES['photo']['name']);
  srand(time());
  $random = (rand()%99999999);
  $newname="$random"."$pic";
  $filename = "images/productphotos/". $newname;
  $filename1 = "images/productphotos/small/". $newname;
  imagejpeg($tmp,$filename,100);
  imagejpeg($tmp1,$filename1,100);
  imagedestroy($src);
  imagedestroy($tmp);
  imagedestroy($tmp1);
  }}
  }
?>
I'm wondering if the FILE part at the top of that second script isn't catching the contents of the session properly.

Get this sorted... and I'm done!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

Update... but still not there yet.

Code: Select all

 echo "$pic";
Produces:
[text]6337011tshirt.png[/text]
On the page that uses the Sessions only.
So I know it's passing thru.
But how do I convert "$pic", into the full scale:

Code: Select all

$pic=($_FILES['photo']['name']);
And make it see it as a file and extension?

Coz it isn't doing it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

Plan B is to just let them upload the 'data', and once paid and done, they can then upload their image.
I've been reading that storing images in Sessions is "stupid".

My ideal is to be able to do that somehow, but there doesn't seem to be a clean way of doing it. Particularly if their image is around 3-4m.

So the solution might be what I have just said: DON'T! And make them do it in a second stage.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Session not working - where have I gone wrong?

Post by genix2011 »

Hi,

sorry had a lot to do. You should upload the file right away, and only save the path to the file in the session, if the payment was not successfull you could just delete the file again.
Storing files in session is bad.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Session not working - where have I gone wrong?

Post by simonmlewis »

I'm more thinking about if they uploaded the file, and got to PayPal, and then thought - no, don't want to do that, and just closed their browser.
I'd have files on the server redundant.

I've gone ahead and done it differently, not storing any files. They create their item, pay, then are taken to a page where it is made live, and they are told you NOW upload their main image.

I really appreciate the time you have taken to help me with all this.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply