Crontab for receiving udp messages in php - php

I am trying to accept a udp message and insert it into the database in php. i am doing it in server.php. i want server.php to run in the background and i am doing it through crontab. the entry for the crontab is */1 * * * * /Applications/bin/php5/php -f /Applications/MAMP/htdocs/Path/server.php. My question is that it works fine when the I run it for first time but when I restart the MAMP (apache/php/MySQL servers) it does not work. Here is the code for server.php
<?php
include('db_connection.php');
$socket = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
socket_bind($socket,$ip_address,$port);
while(1)
{
$IP = '';
$PORT = 0;
socket_recvfrom($socket,$buffer,1024,0,$IP,$PORT);
$query = "INSERT INTO message_table VALUES ('$buffer', '$IP' , '$PORT')";
$result = mysql_query($query) or die("Insertion failed" . mysql_error());
}
socket_close($socket);
?>
I am using MAC OSX 10.6
How can I do that or is there any approach to do this.
Any help will be appritiated.

Related

How do I use sphinx search with PHP?

I am still a programming newbie, please keep that in mind.
I installed SphinxSearch on Linux Mint. I followed instructions from a Digital Ocean tutorial. I created a configuration file (sphinx.conf) as follows:
source mySource{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = mypass
sql_db = test
sql_query = SELECT id, uname FROM users
sql_attr_uint = id
sql_attr_string = uname
sql_port = 3306
}
index test{
source = mySource
path = /var/lib/sphinxsearch/data
docinfo = extern
}
searchd{
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
pid_file = /var/run/sphinxsearch/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /var/lib/sphinxsearch/data
}
My PHP File
<?php
$conn = new mysqli('localhost','root','mypass','test',9306);
if($conn->connect_error){
die("Could not connect");
}else{
echo "You are connected!";
}
$query = $conn->query("SELECT * FROM test WHERE MATCH('hsmith')");
echo $query->error;
?>
PHP is throwing an error stating I am using a "Non-Object"
Any solution? Am I doing somethin entirely wrong?
Most likely the problem is in
$conn = new mysqli('localhost','root','mypass','test',9306);
As per http://php.net/manual/en/mysqli.construct.php when you pass 'localhost' in the 1st param mysqli tries to use unix socket, not a TCP socket and the port number therefore gets just ignored, i.e. you probably connect to your mysql instance instead of sphinx which then causes the problem you get.
Try:
$conn = new mysqli('127.0.0.1','','','test',9306);
instead.
Please also be aware that
echo $query->error;
is wrong since mysqli_result (http://php.net/manual/ru/class.mysqli-result.php) returned by query() does not have property 'error', you probably meant
echo $conn->error;

Longer than usual phpagi running time

I'm using some PHPAGI in asterisk that are working on some servers. But when I used them in my new server they worked like usual for only 3 days and now they take longer than expected. Here is a sample.Any suggestion is appreciated.
#!/usr/bin/php -q
<?php
require('phpagi.php');
error_reporting(E_ALL);
$agi = new AGI();
$_callerId = $agi->get_variable("CALLERID(num)");
if(strlen($_callerId)>4)
{
$con=mysqli_connect(SomeServer);
$result = mysqli_query($con,"Select caller_id,extention from record_call order by id desc limit 50");
while($row = mysqli_fetch_array($result)) {
$tempCallerId = $row['caller_id'] ;
$tempExtention = $row['extention'] ;
if($tempCallerId==$_callerId)
{
$agi->set_variable('exExtention',$tempExtention);
mysqli_close($con);
return;
}
};
$agi->set_variable('exExtention','new');
mysqli_close($con);
?>
I got it. Connecting to MySQL took more than usual and it was because of the dns set in elastix. I removed it and now it's working as expected.

Mysql database connection file errors

I am on an Amazon ec2 instance and I have a database that I am trying to access via a PHP script. I am echoing out tests through the script so i seem to get to the end but I am not getting any errors. Whenever I change my localhost to my IP it says: Connection error: Can't connect to MySQL server on 'MY IP' (110). I have tried binding my localhost and server server IP to my.cnf file and still the same result. Are there any other configurations I am missing? I have never had this much trouble before but this is my first time working on an amazon ec2 Linux instance.
<?php
$con=mysqli_connect("localhost","root","password","database");
if (!$con)
{
die("Connection error: " . mysqli_connect_error());
}
echo "testing1";
$query = "SELECT id, email FROM user_key ORDER BY email DESC LIMIT 10;";
echo "here";
$result = mysql_query($query);
echo "here2";
while($row = mysql_fetch_assoc($result))
{
echo $row['id']." ";
echo $row['email']." ";
}
echo "last";
?>

how to implement a cronjob completely?i want to know step by step procedures

i want to implement a cronjob.
this is my script
#!/usr/bin/php -q
<?php
try {
$dbh = mysql_connect('localhost', 'root', 'portugal')
or die("Unable to connect to MySQL");
$selected = mysql_select_db("AppUsers",$dbh)
or die("Could not select examples");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$dbh->query("UPDATE conditions
SET status='F'
WHERE
category='b'");
its location is /var/www/laravel_app/public/check.php
i make a cronjob by typing the command crontab -e. and i pasted this code at the end and saved it.45 13 * * * /usr/bin/php /var/www/laravel_app/public/check.php
but there is no change in my table after this time.help.tysm in advance
this is how i do it in my backup server
0 7 * * 1-5 wget --quiet -O /dev/null "http://10.29.110.10/files/sync_check.php"

Query not executing. No errors

$query = "SELECT `ip` FROM `banned` WHERE `ip` = '$ip'";
$retval = mysqli_query($conn, $query);
if(!$retval){
die("Could not Execute Query: " . mysqli_error($conn));
} else {
if(mysqli_num_rows($retval) == 0){
echo "test";
} else {
header('Location: http://www.teutonic-development.net/index.php?p=banned');
}
}
when I'm running this code all that's printed out is: "Could not Execute Query:"
I really have absolutely no idea why it's doing this. I'm connecting fine in my init.php file. Which is where this file is.
My other script which just adds a log entry works fine. And if I run my $query in phpmyadmin's sql interpreter it runs perfectly fine (when I replace the $ip part with an actual ip of course)
Any suggestions?
Normally one would say that hey your query failed to execute story finish. But this case is interesting.
Your code is
die("Could not Execute Query: " . mysqli_error($conn));
and your error message is
Could not Execute Query:
Notice even though you have mysqli_error($conn) but there is no mysql error being shown. That confirms 100% that $conn is not properly established (contrary to what you think)
So take a look at your code again and see if $conn is really a mysqli resource and is available to your file in proper variable scope.

Categories