Sunday 1 September 2013

catch fatal error information in older php

catch fatal error information in older php

We have an old php instance running on a server. php 5.1.6 precisely. But
it is handling all payments and some other critical stuff so there is no
plan of upgrading it as of now. There are a couple of crons running. The
requirement is to catch fatal errors. However as we know, function
error_get_last which does the job of gathering error information, was not
available in that version of php.
I have scoured the web for any method of catching the fatal error, but
have not succeeded yet. There is a way to do that mentioned by someone
here http://php.net/manual/en/function.error-get-last.php, however that
did not work. Please help.

Serialization issues while sending struct over socket

Serialization issues while sending struct over socket

I am developing a Client/Server based on UDP I want to send different
messages to the client from the server. There are different C structures
defined for each message.
I would like to understand what is wrong in the way I am serializing the
data.
struct Task
{
int mType;
int tType;
int cCnt;
int* cId;
char data[128];
};
Serialization/Deserialization functions
unsigned char * serialize_int(unsigned char *buffer, int value)
{
buffer[0] = value >> 24;
buffer[1] = value >> 16;
buffer[2] = value >> 8;
buffer[3] = value;
return buffer + 4;
}
unsigned char * serialize_char(unsigned char *buffer, char value)
{
buffer[0] = value;
return buffer + 1;
}
int deserialize_int(unsigned char *buffer)
{
int value = 0;
value |= buffer[0] << 24;
value |= buffer[1] << 16;
value |= buffer[2] << 8;
value |= buffer[3];
return value;
}
char deserialize_char(unsigned char *buffer)
{
return buffer[0];
}
Sender side code to serialize the structure
unsigned char* serializeTask(unsigned char* msg, const Task* t)
{
msg = serialize_int(msg,t->mType);
msg = serialize_int(msg,t->tkType);
msg = serialize_int(msg,t->cCnt);
for(int i=0; i<t->cCnt; i++)
msg = serialize_int(msg,t->cId[i*4]);
for(int i=0; i<strlen(data); i++)
msg = serialize_char(msg,t->data[i]);
return msg;
}
Receiver side code to de-serialize data
printf("Msg type:%d\n", deserialize_int(message) );
printf("Task Type:%d\n", deserialize_int(message+4) );
printf("Task Count:%d\n", deserialize_int(message+8));
Output
Msg type:50364598 //Expected value is 3
Task Type:-2013036362 //Expected value is 1
Task Count:1745191094 //Expected value is 3
Question 1:
Why is the de-serialized value not same as expected?
Question 2:
How is serialization/de-serialization method different from memcpy?
Task t;
memcpy(&t, msg, sizeof(t)); //msg is unsigned char* holding the struct data

merge arrays without reindexing keys

merge arrays without reindexing keys

I have two arrays that I want to merge recursively, so adding arrays is
not an option. This is simple example without multilevels to demonstrate
the problem:
$a1 = Array(
5 => 'pronoun'
)
$a2 = Array(
2 => 'verb',
3 => 'noun'
)
$r = array_merge_recursive($a1, $a2)
And I want to get that resulting array:
Array(
5 => 'pronoun'
2 => 'verb',
3 => 'noun'
)
My problem is that array_merge_recursive function reindixes keys, and I
get the following:
Array(
0 => 'pronoun'
1 => 'verb',
2 => 'noun'
)
I understand that's happening because all my keys are numeric. So I tried
to make them string when adding but it doesn't seem to be working
properly:
$a1[(string)7] = 'some value';
The key - 7 - is still number, or at least that's how it is displayed in
debugger - $a1[7] and not $a1['7']. Any advice?

Codeigniter session not saving userdata and creating duplicate sessions

Codeigniter session not saving userdata and creating duplicate sessions

I'm trying to send my username and password through ajax to the database
for verifying the user but I see CI is creating duplicate sessions for a
single request. Also when I'm trying to set the userdata through
session->set_userdata it is not setting the data. This is confirmed as
neither firebug nor my session database shows the data. Also since no data
is getting set in the session I do not get anything back in my ajax
response to show in the ajax response success
Please guide me what am I doing wrong here as the session cookies are
created even if the username and password combination is wrong.
model
function validate_login()
{
$this->db->where('Useremail',$this->input->post('username'));
$this->db->where('Password',$this->input->post('password'));
$result = $this->db->get('users');
if ($result->num_rows == 1) {
return true;
}else{
return false;
}
}
controller
function validate_login_user()
{
$this->load->model('data_model');
if ($this->data_model->validate_login())
{
$data = array (
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
return = $this->session->userdata('username');
}
else
{
return false;
}
}
MY view
<script>
$(function(){
$('.login').click(function(){
var username = $('#username').attr('value');
var password = $('#password').attr('value');
if (username == "" || password == ""){
$('.LoginErrormsg').css('display','block');
}
if (username !== "" && password !== ""){
$.ajax({
url:"<?php echo site_url('home/validate_login_user');?>",
type:'POST',
data:{username:username,
password:password},
success:function(){
$('#sitelogin').html(msg);
$('.logout').css('display','block');
}
});
return false
}else{
return false;
}
});
})
<div id="sitelogin">
<input type="text" class="loginbox" name = "login_username" id =
"username" placeholder="Email address / Username" />
<input type="password" class="loginbox" name = "login_password"
id="password" placeholder="Password" />
<div class="LoginErrormsg">You forgot to enter Username and
Password</div>
<button class="login" name = "login">Login</button>
<button class="logout" name = "logout">Logout</button>
</div>

Saturday 31 August 2013

install karmasphere plugin in netbeans

install karmasphere plugin in netbeans

I want to run a hadoop mapreduce program (for example WordCount) with
netbeans. I found that there is "karmasphere studio for hadoop" that is a
plugin in netbeans. I follow the bellow instruction to install the plugin
: http://www.hadoopstudio.org/home/installation-guide/ but when I add the
plugin name and url, the "karmasphere studio" does not appearnce in my
available plugins, so I can not download and install it. I have netbeans
7.3.1 with jdk7. anybody know what should I do?

Python os.popen requires an integer?

Python os.popen requires an integer?

I'm running python 2.7.3 and doing some basic stuff involving the os module.
import os
def main():
f= os.popen('cat > out', 'w',1)
os.write(f, 'hello pipe')
os.close(f)
main()
Based on examples I've seen I would expect the code to work, but the
interpreter gives this error:
Traceback (most recent call last):
File "./test.py", line 11, in <module>
main()
File "./test.py", line 8, in main
os.write(f, 'hello pipe')
TypeError: an integer is required
Okay, off to the documentation. The help page says:
write(...)
write(fd, string) -> byteswritten
Write a string to a file descriptor.
fd seems to stand for file descriptor. Presumably this is what you get
when you do something like:
file = open('test.py')
No surprise, online documentation says the exact same thing. What's going
on here?

VB Script to launch the site and change the cookie value

VB Script to launch the site and change the cookie value

I need a VB script to launch a website and change its JESSIONID(which is
available in the cookie editor). It has to contentiously change the value
and launch the website.