Welcome Guest, Not a member yet? Register   Sign In
Weird behavior
#1

[eluser]Fierymind[/eluser]
Hi,

I have built complete billing and support system for web hosting business using CI, everything work great.

There is only 1 thing driving me crazy, may be its not related to CI, but you may share your thoughts in that:

In the last step of web hosting order process, a function prepares a URL of the credit card processor, set a value for session variable 'new_invoice_id' and go to credit card processor website

Code:
$payment_page = ".....";
$this->session->set_userdata('new_invoice_id',$invoice_id);    
header('Location: '.$payment_page);
//end of function

after payment, the credit card processor redirect user back to another function in the same controller, here is the top part of it:

Code:
function new_paid($payment_method,$receipt_id) {
      
$invoice_id = $this->session->userdata('new_invoice_id');
          
if (!$invoice_id) {
            exit('No Invoice ID provided');
}
//rest of order process ............

so if I could not get the $invoice_id from session, the program should halt and print 'No Invoice ID provided' or script will process the order and do many things.

in about 15% of orders, the user get the message 'No Invoice ID provided' AND in the same time order completed (order get status 'Piad' in MySQL db, create new receipt, send email, ..etc .. but does not show 'thank you' page, the output only 'No Invoice ID provided')!

and that means, the exit() executed and program continue to execute! further more, the value $invoice_id IS retrieved successfully and used to complete the order!


am I clear? .. here is again:

1. $invoice_id is retrieved from session correctly.
2. but if (!$invoice_id) {exit();} executed (!?)
3. PHP continue to execute the function disregarding the called exit() and stopped just before last $this->load->view()(!?)

any idea?
#2

[eluser]Cro_Crx[/eluser]
Firstly you need to make sure that the session data is being saved. Maybe run through a few times and print all of the session variables to check.
Code:
<?php print_r($this->session->userdata);?>
If that's displaying correctly every time then it should work.

Could it be possible that the 15% of users are using a particular browser or same OS ? For example it might be a problem with IE8 sessions specifically or some other browser/platform issue?
#3

[eluser]Fierymind[/eluser]
I do not think problem in session, because program could receive the value and save it! so in 100% of cases the program could get the session value.

sometimes (again, we get the session value $invoice_id in all cases) the "if" statment execute

Code:
if (!$invoice_id) {exit(); }

and even after exit(); print the output, the program continue to work!


so I have here 2 problems:

1. why $invoice_id return false in the if statment while its really have value and this value is used after that in the function? ($invoice_id is string, like: '55-new-333-999').

2. why program continue exeute after exit() is called and return an output?
#4

[eluser]Cro_Crx[/eluser]
If the session is fine then it shouldn't evaluate to false. I would suggest using empty instead of an exclamation mark as your testing to make sure that the invoice id is set.

Code:
if (empty($invoice_id)) {exit(); }

No code should be executed after exit() although anything already processed will be output to the screen. So anything above the exit will still be visible. If it's still running code below the exit then something is really wrong. No idea what though!
#5

[eluser]Fierymind[/eluser]
I'll try your advice about empty, for now .. see this wonder thing!

I have modified the return function to be:

Code:
function new_paid($payment_method,$receipt_id) {
      
          $invoice_id     = $this->session->userdata('new_invoice_id');
          
          if (!$invoice_id) {
          
        $this->email->from($company_email, $company_name);
        $this->email->to($company_admin_email);

        $this->email->subject('Something wrong during order process');
        $this->email->message('invoice: '.$invoice_id.' | IP:'.$_SERVER['REMOTE_ADDR']);

            $this->email->send();
              exit('No Invoice ID provided');    
              
          }


I got an email and the $inovice_id is printed ON it!??
#6

[eluser]Cro_Crx[/eluser]
[quote author="Fierymind" date="1258499097"]I got an email and the $inovice_id is printed ON it!??[/quote]

Well yeah, you're sending the message and then afterwards displaying the exit message, that's what should happen, am I missing something? If the exit was above the send line you wouldn't receive an email. The exit only stops the script from continuing, it won't stop anything that's already been run up to that point.

Also CI has an inbuilt show_error function which displays a nicer error to the user. Can call it like this:

Code:
show_error('No Invoice ID provided')

You can also customise the look of the error page. It's in the errors folder.
#7

[eluser]Fierymind[/eluser]
Thank you for reply .. I'm really losing my mind

in my last post, I said

Code:
if (!$invoice_id) {

//code

$this->email->message('invoice: '.$invoice_id.' | IP:'.$_SERVER['REMOTE_ADDR']);
$this->email->send();

//code..
}

so email will not sent unless $invoice_id == false .. but in the same time, email is sent and $invoice_id have a value and I got it written in the email body!

and Thank you about show_error() hint ..
#8

[eluser]Fierymind[/eluser]
Here is the full function

Code:
function new_paid($payment_method,$receipt_id) {
      
          $invoice_id     = $this->session->userdata('new_invoice_id');
          
          if (empty($invoice_id)) {
          
            $this->email->from($company_email, $company_name);
            $this->email->to($company_admin_email);

            $this->email->subject('Something wrong during process order');
            $this->email->message('invoice_id: '.$invoice_id.' | receipt: '.$receipt_id.' | IP:'.$_SERVER['REMOTE_ADDR']);

            $this->email->send();
            
              show_error('<b>new_ERROR:</b><br> No Invoice ID provided!<br>a Report has been sent to Administrator!');
              exit();
              
          }
          
              $update['o_receipt_id'] = $receipt_id;
              $update['o_payment_method'] = $payment_method;
              $update['o_status'] = 'paid';
          
              $this->session->unset_userdata('new_invoice_id');
          
              $this->db->where('o_invoice_id',$invoice_id);
            $this->db->update('orders_new', $update);    
            
                  if ( $this->session->userdata('user_id') > 0) {
          
                      redirect('user/order/'.$this->session->userdata('user_id'));
          
                  }    
                  
            $data['main_menu'] = 'empty';
            $data['sub_menu'] = 'empty';    
            $data['main'] = '_thank_you';
            $data['side'] = 'empty';
            $data['title'] = 'Order Completed';
            $data['page_title'] = 'Order Completed';                

            $this->load->view('orders',$data);                                                
      
      }

after exit(), everything before $this->load->view('orders',$data); is running fine with $invoice_id have good value
#9

[eluser]Cro_Crx[/eluser]
You could just wrap the other stuff in an else statement, preventing it from being run:

Code:
function new_paid($payment_method,$receipt_id) {
      
          $invoice_id     = $this->session->userdata('new_invoice_id');
          
          if (empty($invoice_id))
          {
          
            $this->email->from($company_email, $company_name);

              //////////////////////////////////
            
              show_error('<b>new_ERROR:</b><br> No Invoice ID provided!<br>a Report has been sent to Administrator!');
              exit();
              
          }
          else
          {
              $update['o_receipt_id'] = $receipt_id;
               ///////////////////////////////////////
            $this->load->view('orders',$data);                  
          }                            
      
      }
#10

[eluser]Fierymind[/eluser]
I just fixed this weird issue, I removed this line:


Code:
$this->session->unset_userdata('new_invoice_id');


looks like CI does not like to use and unset session info in the same controller method!




Theme © iAndrew 2016 - Forum software by © MyBB