A Way to Determine Browser Width in PHP Without JavaScript

A way to determine browser width in PHP without javascript?

There is no way to do this. If you want to detect what device is being used then you should examine $_SERVER['HTTP_USER_AGENT'] for clues.

Get browser width using php

You can't do that this way, here is a way to do it :

<?php
session_start();
if(isset($_POST['width'])){
$_SESSION['screen_size'] = array();
$_SESSION['screen_size']['width'] = intval($_POST['width']);
$_SESSION['screen_size']['height'] = intval($_POST['height']);
}


if(!isset($_SESSION['screen_size'])){
?>
<html>
<head>
<script>
function getSize(){
document.getElementById('inp_width').value=screen.width;
document.getElementById('inp_height').value=screen.height;
document.getElementById('form_size').submit();
}
</script>
</head>
<body onload='getSize()'>
<form method='post' id='form_size'>
<input type='hidden' name='width' id='inp_width'/>
<input type='hidden' name='height' id='inp_height'/>
</form>
</body>
</html>


<?php
}else{
var_dump($_SESSION['screen_size']);
}

This is a simple way, and the page will reload the first time.

You may want to use AJAX.

Also, if someone refuses sessions cookies, this would loop forever, better test if the browser accepts cookies.

PHP If statement on browser width w/bootstrap

You can't access browser width (or any other user properties) with PHP. The only thing possible would be to set the width in a cookie (with JS) and then read it with PHP. However, this works only on the second request, as PHP gets executed before JS.

I do suggest you read something about responsive webdesign and you might find out you don't actually need this.

Getting the screen resolution using PHP

You can't do it with pure PHP. You must do it with JavaScript. There are several articles written on how to do this.

Essentially, you can set a cookie or you can even do some Ajax to send the info to a PHP script. If you use jQuery, you can do it something like this:

jquery:

$(function() {
$.post('some_script.php', { width: screen.width, height:screen.height }, function(json) {
if(json.outcome == 'success') {
// do something with the knowledge possibly?
} else {
alert('Unable to let PHP know what the screen resolution is!');
}
},'json');
});

PHP (some_script.php)

<?php
// For instance, you can do something like this:
if(isset($_POST['width']) && isset($_POST['height'])) {
$_SESSION['screen_width'] = $_POST['width'];
$_SESSION['screen_height'] = $_POST['height'];
echo json_encode(array('outcome'=>'success'));
} else {
echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info"));
}
?>

All that is really basic but it should get you somewhere. Normally screen resolution is not what you really want though. You may be more interested in the size of the actual browser's view port since that is actually where the page is rendered...

how to PHP if condition with browser width?

You need JavaScript (taken from http://stackoverflow.com/questions/4180134/how-to-get-users-screen-resolution-with-php and http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php)

var screenWidth = window.screen.width,
screenHeight = window.screen.height;
You can then send it to the server via Ajax (with an XmlHttpRequest).

See also the MDC window.screen docs.

Get browser window size/resolution with PHP

Cannot be done with PHP. Even if it could, this CSS is a far better solution.

You have encountered the most annoying CSS problem in existence. There seems to be as many answers to this question as there are people asking it. The above link is the best, most cross-browser friendly solution I have found for this issue.

Don't let the part about the footer confuse you. If you don't want a footer, it will still work perfectly.

Change php property based on browser width

I would suggest getting the size of the viewport with javascript and then sending them to the php on load. Then write some code to check the size and set "calender_type" accordingly.

var viewportWidth = $(window).width();
var viewportHeight = $(window).height();

send the code to the php page with ajax

$.ajax({url: "page.php", type: "post", data: {"size": size}})

you would then return the calender at its proper size. and place it into a designated container.

.done(function(html) {
$('#someContainer').html(html);
}(;


Related Topics



Leave a reply



Submit