PHP SVG Check with fallback, possible? - php

is it possible to check (with PHP) if the browser supports SVG?
like ...
if( BROWSER support SVG )
{
$iT = 'svg'; // Icon type
}
else
{
$iT = 'png'; // Icon type
}
in HTML code ...
<img src="icons/home.<?=$iT?>" class="icon" />
EDIT:
How about to check the browser and the version? Good idea?
$data['browser'] = strtolower($data['browser']);
if ($data['browser'] == 'firefox' && (int)$data['browser']['version'] >= 10)
$iT = 'svg';
elseif ($data['browser'] == 'safari' && (int)$data['browser']['version'] >= 5)
$iT = 'svg';
.... and so on
PS: Did anybody know a nice SVG-Browser-Support-List?

You could probably do the check using JavaScript and Raphael, and then send that back to the server.

Related

php check if browser tab was opened with "target=" - and what target it was

I am opening two windows in one session, which should have different behaviours. As the windows open at the same time, they are using th same options and interfering with each other. Therefore I would need to check in the code, how they were opened.
If it was opened with something like
window.open(href, target='pdf');
I would want to check this
if(tab.target = "pdf")
{
$check_target = 1
}
else
{
$check_target = 2
}
now "tab.target" does not exist - can I achieve this somehow?
Thanks!
Max
I would add a something to the url like this:
window.open(href+'?mytarget=pdf', target='pdf');
Then in your php code:
if($_GET['mytarget'] == "pdf")
{
$check_target = 1;
}
else
{
$check_target = 2;
}

PHP image comparison

img_1 is created by PHP and img_2 is saved on server. I'm trying to compare those to images to see if they're different, I tried this but it doesn't work.
$script_img = imagecreatetruecolor(2390, 2400);
$web_img = imagecreatefrompng("URL_TO_IMG");
if ($script_img==$web_img ) {
echo "SAME";
}
else{
echo "DIFFERENT";
}
Next example works but when I call imagepng PHP creates image in browser or weird letters (if headers isn't set to image/png) and I don't want that.
$script_img = imagecreatetruecolor(2390, 2400);
$web_img = imagecreatefrompng("URL_TO_IMG");
$rendered = imagepng($web_img);
if ($script_img==$rendered ) {
echo "SAME";
}
else{
echo "DIFFERENT";
}
I also tried file_get_contents($script_img) == file_get_contents("URL_TO_IMG") but it doesn't work.
Using md5(file_get_contents(imagecreatetruecolor(2390, 2400))) == md5(file_get_contents(imagecreatefrompng("URL_TO_IMG"))) works but I doubt that is the best/correct way to compare 2 images.
What is the best/correct way to compare images in PHP?
Why don't you try comparing MD5 Hash of the two images.
$md5LocalImg = md5(file_get_contents($script_img));
$md5WebImg = md5(file_get_contents($web_img));
if ( $md5LocalImg == $md5WebImg ){
echo("SAME");
}
else{
echo("DIFFERENT");
}

Cant get Php restrictions to work with HTML input tag for song upload

Hello I'm trying to get restrictions to work with my php upload script to make
sure people are only uploading music nothing els but when I run in the browser
I always get upload faild all post my code below
<?php
// This PHP5 file is used to move an uploaded song file to its final
// destination. The song is scaled as part of the process.
// A normal HTML upload form will serve as the user interface for the
// upload. The song file should be submitted using a field named
// "song".
// set database connection
require("------.php");
// lets get our posts //
$song = $_FILES['song'];
// folder that will hold songs
$songpath = "songs/";
// song-file pathname
$songpath .= $song["name"];
//---------------------------------------------------------------
// this file is going to add restrictions to the song form
$ftype = $_FILES["song"]["type" ];
$xerror = $_FILES["song"]["error" ];
$xsize = $_FILES["song"]["size" ];
if (($ftype == "audio/mp3" )
|| ($ftype == "audio/ogg" )
|| ($ftype == "audio/wav" )
|| ($ftype == "audio/midi" ))
{
$it_is_a_song = 1;
}
else
{
$it_is_a_song = 0;
}
if ($it_is_a_song && ($xsize < 20971520) && ($xerror == 0))
{
$it_is_good = 1;
}
else
{
$it_is_good = 0;
}
//-----------------------------------------------------------------
print <<<END
<html>
<title>You Must Party Upload Results</title>
<body>
END;
// move the file from the tmp folder to the song folder
if ( $it_is_good &&
move_uploaded_file ($song['tmp_name'], $songpath))
{
print "<p>Upload succeeded thank you</p>\n";
}
else
{
print "<p>Upload failed, sorry</p>\n";
}
print <<<END
<p>
To continue, click here.
</p>
</body>
</html>
END;
?>
$_FILES["song"]["type"] will return mime type and song mime type doesn't exist so it'll always fail.
Instead use audio, eg. audio/ogg.

handling php code

morning. I am wanting to take all segments of php code out of a file located on my local server. Problem is i dont seem to be getting anywhere, no php errors just browser errors.
$file_contents = "<xmp>".file_get_contents("../www.cms.actwebdesigns.co.uk2/pageIncludes/instalation/selectMainPages.php")."</xmp>";
if(preg_match_all("#<\?php((?!\?>).)*#is", $file_contents, $matches))
{
foreach($matches[0] as $phpCode)
{
$code = "<xmp>".$phpCode."\n?></xmp>";
}
}
echo "dsds";
?>
could someone please point me in the right direction?
working with this:
$file_contents = token_get_all(file_get_contents("../www.cms.actwebdesigns.co.uk2/logged.php"));
$start=0;
$end=0;
$segmentArray = array();
foreach($file_contents as $key => $token)
{
$tokenName = token_name($key);
if($start==0 && $end==0 && $tokenName=="T_OPEN_TAG")
{
$start=1;
}
if(start==1 && $end==0 && $tokenName!="T_CLOSE_TAG")
{
$entryNo = count($segmentArray);
$segmentArray[$entryNo][] = $token;
}
if($tokenName=="T_CLOSE_TAG")
{
$start=0;
}
}
You might want to tokenize the PHP script using the Tokenizer extension:
http://php.net/manual/en/book.tokenizer.php
The extensions is built into PHP since PHP v4.3.0.
$tokens = token_get_all(file_get_contents($file));
http://www.php.net/manual/en/function.token-get-all.php
Not sure how to use this. Puts all code into an array. For me to use it wouldn't i have to implode it or something then im back to square one?

How can I use a php file's output with Ajax?

I was following this tutorial.
I need to use a php file's ouput in my HTML file to dynamically load images into a gallery. I call
function setOutput()
{
if (httpObject.readyState == 4)
document.getElementById('main').src = httpObject.responseText;
alert("set output: " + httpObject.responseText);
}
from
function doWork()
{
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "gallery.php?no=0", true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
However, the alert returns the php file, word for word. It's probably a really stupid error, but I can't seem to find it.
The php file:
<?php
if (isset($_GET['no'])) {
$no = $_GET['no'];
if ($no <= 10 && $no >1) {
$xml = simplexml_load_file('gallery.xml');
echo "images/" . $xml->image[$no]->src;
}
else die("Number isn't between 1 and 10");
}
else die("No number set.");
?>
If the alert is returning the contents of the PHP file instead of the results of executing it, then the server is not executing it.
Test by accessing the URI directly (instead of going via JavaScript).
You probably need to configure PHP support on the server.
Your Server doesn't serve/parse PHP files! You could test your JavaScript code by setting the content of gallery.php to the HTML code you want to receive.

Categories