|
|
#241 (permalink) | |
|
Senior Member
Join Date: May 2007
Location: pacinian corpuscle.
Posts: 1,868
|
Quote:
I'm still not 100% on what the server does. Just logs identifiers and sends them to an Apple gateway for push notifications? |
|
| (Offline) |
|
|
|
#243 (permalink) |
|
Member
Join Date: Mar 2006
Posts: 42
|
Are you trying to remove duplicates in the $dev variable or duplicate lines in the file? If it's duplicate lines in the file, this should do it:
Code:
$lines = file($filename);
if (!in_array($front.$deviceToken.$back,$lines))
{
//append to file code in here
}
Last edited by s1lentslayer; 05-05-2009 at 11:26 PM. |
| (Offline) |
|
|
|
#244 (permalink) | |
|
Administrator
Join Date: Mar 2008
Location: Detroitish
Posts: 1,654
|
Quote:
|
|
| (Offline) |
|
|
|
#245 (permalink) |
|
Administrator
Join Date: Mar 2008
Location: Detroitish
Posts: 1,654
|
Ok questions.
First, can you just stick /n in the middle of a string like Code:
$back1 = '</device>\n'; Second I'm trying to generate a feed that looks like this Code:
<?xml version="1.0" encoding="utf-8"?> <root> <device><deviceToken>c32f8b7c e8d02a51 ce296c62 d3da4cb3 84fed78d b24545db fd949a73 132f2b50</deviceToken></device> <device><deviceToken>c32f8b7c e8d02a51 ce296c62 d3da4cb3 84fed78d b24545db fd949a73 132f2b50</deviceToken></device> </root> Code:
<?php
$deviceTokenUF = $_GET['dev'];
$deviceToken = substr($deviceTokenUF, 1, -1);
$filename = 'tokenLog';
$front1 = '<device>';
$front2 = '<deviceToken>';
$back2 = '</deviceToken>';
$back1 = '</device>';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $clientMessage will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $clientMessage to our opened file.
if (fwrite($handle, $front1.$front2.$deviceToken.$back2.$back1) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($clientMessage) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
I'm a php dummy dumb, so any help would be useful. Also bear in mind that this might eventually become a hightraffic piece of code so tight and fast is important.
__________________
KATGIPHONE Donation ![]()
|
| (Offline) |
|
|
|
#246 (permalink) |
|
Administrator
Join Date: Mar 2008
Location: Detroitish
Posts: 1,654
|
Ok so here's where I am now, the line duplication doesn't seem to be detected.
Code:
<?php
$deviceTokenUF = $_GET['dev'];
$deviceToken = substr($deviceTokenUF, 1, -1);
$filename = 'tokenLog';
$front1 = '<device>';
$front2 = '<deviceToken>';
$back2 = '</deviceToken>';
$back1 = '</device>';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $clientMessage will go when we fwrite() it.
if (!$handle = fopen($filename, 'r+')) {
echo "Cannot open file ($filename)";
exit;
}
$lines = file($filename);
$numElements = count($lines);
for($i = 0; $i < $numElements - 1; $i++)
{
print $lines[$i];
print '
';
}
if (!in_array($front1.$front2.$deviceToken.$back2.$back1, $lines))
{
print 'Not In Array
';
for($i = 0; $i < $numElements - 1; $i++)
{
fwrite($handle, $lines[$i]);
}
fwrite($handle, $front1.$front2.$deviceToken.$back2.$back1.'
');
fwrite($handle, $lines[$numElements - 1]);
} else {
print 'In Array
';
for($i = 0; $i < $numElements; $i++)
{
fwrite($handle, $lines[$i]);
}
}
echo "Success, wrote ($clientMessage) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Last edited by hayroob; 05-06-2009 at 01:09 AM. |
| (Offline) |
|
|
|
#248 (permalink) |
|
Member
Join Date: Mar 2006
Posts: 42
|
First, create the file with no entries that starts like this:
Code:
<?xml version="1.0" encoding="utf-8"?> <root> </root> Code:
<?php
$deviceTokenUF = $_GET['dev'];
$deviceToken = substr($deviceTokenUF, 1, -1);
$filename = 'tokenLog';
$line_to_add = "<device><devicetoken>$deviceToken</deviceToken></device>";
//see if this line exists
$lines = file($filename);
if (!in_array($line_to_add,$lines))
{
//doesn't exist
//first pop off last </root> element and add it back later
array_pop($lines);
//now add new device token line
array_push($lines,$line_to_add);
//now add </root> line
array_push($lines,"</root>");
//now combine into one string separated by newlines
$new_lines = implode("\n",$lines);
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
//lock the file for writing
flock($fp, LOCK_EX);
//write string to file
fwrite($handle,$new_lines);
// release the lock
flock($fp, LOCK_UN);
echo "Success, wrote ($clientMessage) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
?>
Last edited by s1lentslayer; 05-06-2009 at 08:33 AM. |
| (Offline) |
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|