PHP UserAgent for IE 10.0 does not match - php

The PHP redirect should working for IE version 8 and below. version 9 is working fine but IE 10.0 is not working with this code (looks like IE 10.0 gets dismissed or etc.):
if ( preg_match("/(?i)msie [1-8]/",$_SERVER['HTTP_USER_AGENT'])) {
header("Location: http://www.XYXYXYX.com");
}
my goal would be, that the redirecting should only work for MS IE 8 and below.
Thanks alot.
EDIT2 / SOLUTION:
i should have changed my code to this:
if ( preg_match("/(?i)msie [1-8]\./",$_SERVER['HTTP_USER_AGENT'])) {
header("Location: http://www.XYXYXYX.com");
}
The trailing dot ensures the version number is only one digit.

I think a more reliable way of redirecting for IE would be to do it via javascript using conditional HTML statements:
<!--[if lte IE8]>
<script>
window.location.assign('http://www.XYXYXYX.com')
</script>
<![endif]-->

Make sure you check the FULL version number.
if ( preg_match("/(?i)msie [1-8]\.0/",$_SERVER['HTTP_USER_AGENT'])) {
header("Location: http://www.XYXYXYX.com");
}
Note the ".0" suffix that gets checked. That way, you'll never need to change the code again for versions of 10, 20, 30 or even 80 that might come.
Ah, well, IE 5.5 was once released...
if ( preg_match("/(?i)msie [1-8]\./",$_SERVER['HTTP_USER_AGENT'])) {
header("Location: http://www.XYXYXYX.com");
}
Check that after the ONE single digit, there is a dot. If it is not a dot, it probably is a digit from a two-digit version number like "10".

preg_match('/(?i)msie (6|7|8|9|10)/',$_SERVER['HTTP_USER_AGENT'])
or
if(preg_match('/(?i)msie [1-9]/',$_SERVER['HTTP_USER_AGENT']))
{
// if IE <= 10
echo "version is less than 10"; //rest of your code
} else if(preg_match('/(?i)msie [10]/',$_SERVER['HTTP_USER_AGENT']))
{
// if IE = 10
echo "version is IE 10"; //rest of your code
}
else
{
// if not 10
echo " other browser"; //rest of your code
}

Related

Compare 2 strings in PHP

I'm in trouble because i can't figured it out how to find my answer.
I'm creating a wordpress website and i have a search engine
I've created an option in my admin panel and code something to write in my option table, but my problem is now
firstly i grab my options from my database with
$mysynonymvalue = get_option( 'synonym-custom' );
I precise that it returne to me something like this ( mango, apple, banana)(this is an example of course)
My Url is something like this :
http://supserwebsite/wordpress/?sfid=2675&_sf_s=toto
Or this
http://superwebsite/wordpress/?sfid=2675&_sf_s=virtualisation cloud devops
so i've created a variable to catch the queries
$motsclefs3= $_GET['_sf_s'];
Now i want to compare the string $mysynonymvalueconvert with $motsclefs3 to find if it match so i write
if (strpos ($mysynonymvalue, $motsclefs3) ){
echo '<script >
$(document).ready(function(){
$(".vc-tabs-li:nth-child(2)").get(0).click();
});
</script>';
}
else{
echo '
<script >
$(document).ready(function(){
$(".vc-tabs-li").get(0).click();
});
</script>';
};
};
The solution seem to work correctly but i can't have the first result, it coompare indeed with all the results but not my first one.
And it doesn't work so fine because with only one letter it return a match ( for example 'a')
Any solution ?
Thanks
*provided url(s) are not accessible.
Solution:
Create a dictionary of option from db and then search the element which you are looking.
So far i move up a bit !So i came with this
I still have
$mysynonymvalue = get_option( 'synonym-custom' );
$mysynonymvalueconvert = preg_split('/[,]+/',$mysynonymvalue);
To grab my words from my database and to convert it into an array.
(the point of this is to get elements that were wrote by an user elsewhere on the admin panel of wordpress)
I also still have
$motsclefs3= $_GET['_sf_s'];
To grab my actual queries ( that will serv me to compare ). I precise that it return me a string. To be more specific, an URL like this (http://mywebsite/wordpress/?sfid=2675&_sf_s=examen) return me (in string) "examen".
Now my point is still to compare if
$motsclefs3;
is inside
$mysynonymvalueconvert
So i've created a "for" loop like this
for ($i = 0; $i <= count($mysynonymvalueconvert); $i++){
if(in_array($motsclefs3, $mysynonymvalueconvert)){
echo'yes';
break;
}
else{
echo 'no';
break;
};
};
But i'm still blocked, this return "yes" only if it match with the first element from
$mysynonymvalueconvert
So any ideas to help me ?
Thanks!

simple if else or preg_match expression

I have an example.php form when posted to perl script example.pl which responds either this
<!-- comments disabled -->
or this
<!-- comments enabled -->
The above response is stored in php get request in $commentstatus variable
Response may be a multiline or single line !
How to build an expression or verify the response using php preg_match or a normal validation for if else condition in php?
You can check for the presence of a string, within a string with strpos().
So the actual code would be:
<?php
if (strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
elseif(strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
function commentsEnabled($str){
preg_match_all('/^.*?<!--\s*comments\s+(en|dis)abled\s*-->.*$/m', $str, $m);
return !count($m[1]) ? null : array_pop($m[1]) == 'en';
}
demo
enlarge image
Credits to #lbu for the SIMPLEST solution who commented under the Op
This works excellently and is the easiest
$content= trim($commentstatus);
if ($content == "<!--commentsdisabled-->"){
print "Dear User Comments are disabled for your access";
}else if($content == "<!--commentsenabled-->"){
print "Dear User Comment added to blog post entry";
}else{
print "You are Banned for Spamming";
}
#degenerate
"Learn how to read the PHP documentation," - rude and assumptious
But your code works, even for line breaks read in from a file.
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
I don't know how it works, because the documentation you linked says to use the 's' switch :
"The s tells the code NOT TO stop searching when it encounters \n (line break)"
This works is with preg_match_all & preg_match !!
It's a plus 1 even with the ending snark!
Not sure what #BetaCoder is on about, but it's obviously not a question.
Not sure if you are simply asking this?
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
Learn how to read the PHP documentation, please:
http://us1.php.net/preg_match

How do I target IE 10 and up with PHP?

I'm using the following code to add some conditional styles to a webpage (I use traditional conditional comments for IE 9 and down):
<?php if (stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10')) { ?>
<link href="/css/ie9.css" media="screen" rel="stylesheet" />
<?php } ?>
The problem is that the style sheet is still not being applied to some versions of IE 10, and definitely not IE 11 preview. Is there a way to target all versions of IE 10 and up? I've only seen formatting that uses [1-9], but [10-11] doesn't seem to be working.
Put some code in your user agent check to see if anything is being returned at all, it may be returning something other than "MSIE 10", it is possible that IE 10+ returns simply IE or some other statement. I have been using javascript to do this very same thing. (despite the cat-calls from the purists about using conditional comments)
Here is my javascript:
function isIE(ver)
{
var myNav = navigator.userAgent.toLowerCase();
var IEVer = parseInt(myNav.split('msie')[1]);
if ((typeof IEVer == "undefined") || (IEVer == null)) {
IEVer = 0;
}
if IEVer = ver {
return true;
}
else {
return false;
}
}
Then in where i'm adding stylesheets is do something like:
if isIE(10) { do something }
Update: according to Microsoft, you should use feature detection instead of browser detection: http://msdn.microsoft.com/en-us/library/ie/hh273397%28v=vs.85%29.aspx

View full site with cookies, javascript and mobile redirect

I have a website(www.website.com) and a mobile site (m.website.com) and I'm trying to allow users to "View Full Site" if they want from mobile. Currently I am using some Javascript to check screen width on full site then redirecting to mobile.
if (screen.width <= 699) {
document.location = "http://m.website.com";
}
This works fine. On the mobile site there is a "View Full Site" button. When you click this it redirects you to my script "force_desktop.php" which sets a cookie and then sends you to the main site.
<?php
setcookie("force_desktop", "1");
header("Location: http://www.mywebsite.com");
?>
Now that we set a cookie and redirected to the main website we need to check for the cookie instead of just checking for the screen width.
Logic
If Cookie force_desktop is found
Then exit
Else
run screen size test
Here is the code I attempted to use but doesn't work. This code is placed in my head.php file which will be run on every page and is placed between the script opening and closing tags.
Attempt 1
if($_COOKIE['force_desktop'] == '1' {
exit;
} else if($_COOKIE['force_desktop'] != '1' {
if (screen.width <= 699) {
document.location = "http://m.website.com";
}
};
Attempt 2
if(isset ($_COOKIE["force_desktop"])){
exit;
else
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
};
Alternative logic that could work
IF Cookie force_desktop is not found AND screen.width <= 699
Then redirect to m.myseite.com
Else
Exit
Note
I have run the following script to make sure a cookie is being placed, and it is.
<?php
print_r($_COOKIE);
?>
I've run out of ideas and know my coding isn't correct, especially the If/Else statement within the If/Else statement. I also am not sure if it is better to use the "isset" to see if the cookie is being used or ($_COOKIES['variable'] == "#"). I'd really appreciate some good feedback on this one.
Thanks in advance for your help and suggestions,
Matt
You're mixing JavaScript and PHP. You're trying to use screen.width in your PHP script, which doesn't make sense. You need to use an echo statement to output the JavaScript into the page. It'll then check the user's screen resolution and do the redirect.
Try this:
if(isset ($_COOKIE["force_desktop"])){
exit;
}
else
{
echo '<script>
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";}
</script>';
};
you should do this test at the top of the php page, and for sure you cannot mix php and java script like this
u can alter this code like
<?php
$flag = 0;
if(isset($_COOKIE['force_desktop']))$flag++;
?>
later in the page use the code as soon as <head> tag starts
..
..
<head>
<?php
if(!$flag){
echo '<script>
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
</script>';
}
?>
You cannot mix javascript and PHP, javascript is front end and PHP is back end. Try something like this:
if( $something ){
Header("Location: somewhere.php");
}

Remember Page Position Using Two Lines of Script

In all major browsers, except Internet Explorer, the following script returns the page to its previous vertical position on reload:
<?php $y = $_COOKIE["y"]; ?> //in head tag before any output
and
<?php
print "<body onScroll=\"document.cookie='y=' + window.pageYOffset\" onLoad='window.scrollTo(0,$y)'>";
Can someone please tell me how I would modify this code to remember the page's vertical position in IE? Thanks.
From w3Schools :
IE 8 and earlier does not support this property, but may use "document.body.scrollLeft" and "document.body.scrollTop" instead.
I use the following code to do basic IE/not-IE browser detection:
if(document.all) { //if IE
//code
} else { //if not IE
//code
}
You should be able to combine this with AlecTMH's document.body.scrollLeft and document.body.scrollTop suggestion to get where you're going. But you're likely going to have to write a function for it and then call that in onScroll().
I'm not exactly a JavaScript wiz, but...
function blah() {
if(document.all) { //if IE
document.cookie='y=' + document.body.scrollTop
} else { //if not IE
document.cookie='y=' + window.pageYOffset
}
}
...might almost be functional code.

Categories