Hi; I have a mobile script from detectmobilebrowsers.com that will redirect the user to my mobile site however I also wish that when the URL "http://mysite.com/?mobile=no" is entered a session will be created that won't redirect the user on every page of my site...

$mobile=$_GET['mobile'];
	if(isset($_SESSION['mobile'])){
		if($_SESSION['mobile']==="no"){
			complete();
		}
	}
	if($_SESSION['mobile']==="no"){
		complete();
	}
	if($_SESSION['mobile']!="no"){
		checkMobile();
	}
	function checkMobile(){
		// MOBILE CODE TAKEN OUT TO SAVE SPACE
		gotoMobile();
	}
	function gotoMobile(){
		echo "<script>window.location='';</script>";
	}
	function complete(){
		echo "<h1 align='center'>Redirecting...</p>";
	}

Sorry if I seem confusing but in short terms: Mobile Detection (which is set)... make session mobile=no if user does wishes to view full site and when that session is created it is checked on everypage (same php script) and if I set my session for no mobile I want that to stay on everypage...

Thanks in Advanced :)

Dani AI

Generated

Two quick points most threads miss: always call session_start() before you touch $_SESSION, and a cookie/session set on one host won’t be sent to a different host or top‑level domain. That explains why ?mobile=no appears to “not stick” when you redirect to a separate mobile host or to a different domain. ’s idea to cache the detection result is good — just make sure the session/cookie is created correctly and early.

A simple, robust flow (place this at the very top of every page, before any output):

<?php
// set cookie domain/path BEFORE session_start() if you need sharing between subdomains
ini_set('session.cookie_domain', '.mysite.com'); // adjust to your domain
session_start();

if (isset($_GET['mobile']) && $_GET['mobile'] === 'no') {
    $_SESSION['mobile_pref'] = 'no';
    // persistent cookie so preference survives browser restarts and works across subdomains
    setcookie('mobile_pref', 'no', time() + 30*24*3600, '/', '.mysite.com');
    // redirect to a clean URL so users don't bookmark ?mobile=no
    header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?'));
    exit;
}

if (!empty($_SESSION['mobile_pref']) && $_SESSION['mobile_pref'] === 'no') {
    // user asked for full site — do nothing
} else {
    // run detection once, set session, then redirect to mobile if needed
    if (is_mobile()) {
        header('Location: "https://m.mysite.com/"');
        exit;
    }
}

Troubleshooting checklist: confirm session_start() runs before any HTML/output; inspect cookies in browser devtools (is the session cookie present, and does its domain match?); if mobile site is on a different domain (example.com vs example.org) you cannot share cookies — use the same domain or fall back to a persistent cookie or server-side preference tied to a login; prefer PHP header() redirects + exit (more reliable than JS); call session_write_close() before long redirects if concurrent requests matter. Also avoid reusing generic session keys (use something unique like mobile_pref) — ’s point about session variables is relevant here.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

As opposed to a separate mobile site you mat want to check out responsive design using media queries. Just a thought.

something like this?

//$mobile=$_GET['mobile'];

// user want normal site
if(isset($_GET['mobile']) AND $_GET['mobile']==="no"){
 		$_SESSION['mobile']="no"; 
		}
// we saved know: normalsite	
if(isset($_SESSION['mobile']) AND $_SESSION['mobile']==="no"){
 		 complete();
		}		
// we saved know: mobilesite
if(isset($_SESSION['mobile']) AND $_SESSION['mobile']==="yes"){
 		 gotoMobile();
		}	
// we  don't know so		
checkMobile();
	

function checkMobile(){
		// MOBILE CODE TAKEN OUT TO SAVE SPACE
		// this is a havy fuction so lets save the result to speed things up nex time
        if (  mobile )
        	{
        	$_SESSION['mobile']="yes";
        	gotoMobile();
        	}
        else {
        	$_SESSION['mobile']="no";
        	 complete();;
        	}
     	}
     	
function gotoMobile(){
		echo "<script>window.location='';</script>";
	}
function complete(){
		echo "<h1 align='center'>Redirecting...</p>";
	}

@ardav I don't like media queries because all cssfiles are loaded even if they are not used and on a mobile device you don't have bandwith to wast. just my 0.02$

Member Avatar for Member #120589

Fair comment - how about starting with css for mobile and scaling up?

I find that on mobile device you have to be very carefull not to put to much info on a page. There just isn't enough room to display more. This requeres a different aprouch to the html and other functonalty -> different .js
Makes more sence to me to put the php that doesn't change in to a sepearte file and include it

Member Avatar for Member #120589

Yes, using RD for complicated sites is probably a bad idea. However, if devised a few simple ones (mobile up) that seem to work OK on mobile and desktop. I try to avoid HTML changes as this should be semantic anyway. If you have loads of images, videos, animations etc, I can see why RD would be a headache, especially if the site is produced desktop down.

Here is an example of session issue. The point of reference, the answer is a session variable, come into play if you define a session variable $ _SESSION [" nextValidSubmission "]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.