[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

how to add a link to the desktop version from the monile site?

HI I currently have a mobile version of my website that works by a php script that detect the USER AGENT replace the css for mobile site and strip a few other things.
I want to add a link in the mobile site to the the desktop site
How Can I do it?

Thank you
0
Nura111
Asked:
Nura111
  • 13
  • 11
  • 5
  • +1
3 Solutions
 
Ray PaseurCommented:
Use the HTML anchor tag?  <a href="desktop.mysite.com"> Desktop Link</a>
0
 
Nura111Author Commented:
the desktop version and teh mobile version has the same URL thats my problem . As I explaines the php script is checking the USER agent so when the user will go to the link isnt its going to reconsize that its a mobile User agent like it did when he intially went to the website from mobile?
0
 
Ray PaseurCommented:
Consider adding something like a URL argument to the link?  But if it's the same URL for both desktop and mobile, and your script detects the browser and configures itself correctly for the different user agents, it would seem that your clients would be OK whether they visited from a mobile or desktop.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ob2sCommented:
Hi,

You might have a look at this relatively simply example of a "canned" solution to see if it will suit your needs::
   http://www.squidoo.com/php-mobile-redirect

Regards
Fred
0
 
liveaspankajCommented:
Hi

you can do this. give a link like this

<a href="http://mysite.com?desktop=1">Desktop Link</a>


Then where-ever you are checking USER Agent, if in the same function add: && !isset($_GET['desktop'])

like if($_SERVER['USER_AGENT'] == 'ASMKDASDJKSKHD' && !isset($_GET['desktop']))

0
 
Ray PaseurCommented:
Perhaps we are not understanding your question.  Let me see if I can paraphrase.

At present, I am on a desktop computer.  The user agent data is this:

Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

If I were to visit the site with an iPhone, the user agent data would look something like this:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko)...

Since the site gets control in PHP after the value is set in $_SERVER["HTTP_USER_AGENT"] and before any browser output is created, there is no inherent reason for separate versions to cover the desktop and the mobile devices.  You said, "the desktop version and the mobile version has the same URL thats my problem."  However it does not seem to be a problem.  Your script would just check the user agent string in the server or environmental variables.
0
 
Nura111Author Commented:
yes but the client still want to have a link to the desktop version as well even if it doesnt make sense..
0
 
liveaspankajCommented:
it makes sense..
did you try what i mentioned.
0
 
Ray PaseurCommented:
But if the desktop version and the mobile version use the same URL, one link fulfills both requirements.  There is not any other link.  There is just one link.  

Do you understand how the HTTP_USER_AGENT string is set by the browser?  The input from the client (JTTP_USER_AGENT) determines the site behavior.  It's really that simple.
0
 
ob2sCommented:
@Nura111
Hi,
Maybe I'm wrong, but isn't the issue that you also want to let the user optionally override HTTP_USER_AGENT with a "preference". That's easily done by setting a cookie to retain the user's preference for the session (or whatever cookie lifetime desired).

The link I posted describes a simple way to do this, but there other ways to implement this behavior, if you don't like that particular implementation. Is this not the behavior you want? Do you not want to use cookies?

Regards,
Fred
0
 
Nura111Author Commented:
Hi guys, thank you so much for the help sorry it took me so much time to answer:
liveaspankaj yes thats exctly what I meant. in order toe Url to not show as /?desktop=1 I will need to to add a redirect to it to htaccess right? or there is another way of doing that?

ob2s: I guess it can work as well. do you know  the advantage of using that ?

Thank you again everybody
0
 
liveaspankajCommented:
why do you want to redirect.
he will click on the link with that URL...you get the value in $_GET and add that to the condition.
yes you can set the value of desktop=1 in the cookie...

TO SET   : setcookie("desktop", 1);
TO READ : $_COOKIE['desktop']

better..post your code here...if you dont know how to code what i mentioned...and me or someone here might give you the right code.
0
 
Nura111Author Commented:
<<why do you want to redirect.
Im currently doing it as ob2s suugest so when the user click the link the url is http://mysite.com?desktop=1"
and I want it to be:http://mysite.com
0
 
liveaspankajCommented:
okay.....
in that case you can redirect
set the cookie...and immediately redirect

if(isset($_GET['desktop'] ) && $_GET['desktop'] == 1)
{
setcookie("desktop",1);
header("Location: http://domain.com");
exit;
}
else if(isset($_GET['desktop'] ) && $_GET['desktop'] == 0)
{
setcookie("desktop", 0);
header("Location: http://domain.com");
exit;
}
and use $_COOKIE['desktop'] to decide which page to show
0
 
Nura111Author Commented:
I really dont understand your suggestion and how to use it..
Can you explain? or maybe Can I use a rewrite rule in the htaccess file?
0
 
liveaspankajCommented:
not sure how you can use htaccess for that...

can you tell what part you didnt understand? apart from header("Location:

that line is a redirect line
0
 
Nura111Author Commented:
the all idea  it will already go the   http://domain.com" because that the URL that in the link.
so why do I need to redirect it again like that.
all I want is to change remove the query strin ?desktop=1 from the url that the user see
0
 
liveaspankajCommented:
user goes to: http://domain.com?desktop=1
how do you send him to  http://domain.com without a redirect. and by send to  http://domain.com i mean, his browser reads  http://domain.com because for server...he is anyway on the same script.

the if conditions will be at the top of the index.php page and will redirect them immediately

if you dont want him to completely see it may be you can use a form

<form method="post">
<input type="submit" name="desktop" value="Desktop Version" />
</form>

and check for $_POST['desktop'] == "Desktop Version" in php and set cookies or session and do what you want without a redirect


if i couldn't explain you well....probably it will save your time if you can hire a coder and pay him $10-20 bucks

:)
0
 
Nura111Author Commented:
ok I got it Thanks . unforgettably Im the coder .
0
 
liveaspankajCommented:
ohhh....
i am extremely sorry
didnt know that
0
 
Nura111Author Commented:
Its ok nothing to be sorry about.
Thank you for the help
0
 
liveaspankajCommented:
welcome :)

hope it solved your problems
0
 
Ray PaseurCommented:
Either I really do not understand this dialog, and/or maybe I am missing something.  

See http://www.laprbass.com/RAY_temp_nura111.php to run the script below.   Try it from an iPhone and a desktop computer like a Mac or a PC.

See also http://www.google.com/search?btnG=1&pws=0&q=Browser+Detection+in+PHP

The point is simply this: There is no need for a different URL for mobile and desktop.  The same URL works for both versions of the site.  You just include() the correct version.  It really is that easy!
<?php // RAY_temp_nura111.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE BROWSER DETECTION IN PHP


// A LIST OF USER AGENTS (INTENTIONALLY OVERSIMPLIFIED)
$agents = array
( 'Windows'   => 'Desktop'
, 'Macintosh' => 'Desktop'
, 'MSIE'      => 'Desktop'
, 'iPhone'    => 'Mobile'
, 'iPod'      => 'Moblie'
)
;

// THE SIGNAL THAT WE KNOW THIS BROWSER
$known   = FALSE;

// LOCATE THE BROWSER AND CHOOSE THE VERSION OF THE WEB SITE
foreach ($agents as $browser => $version)
{
    if (strpos($_SERVER["HTTP_USER_AGENT"], $browser))
    {
        $known = TRUE;
        break;
    }
}

if (!$known) echo "SORRY, I DO NOT RECOGNIZE {$_SERVER["HTTP_USER_AGENT"]}";
if ($known)  echo "YOU ARE USING $browser SOFTWARE, SO YOU GET THE $version VERSION OF THE SITE";

echo PHP_EOL;
print_r($_SERVER["HTTP_USER_AGENT"]);

Open in new window

0
 
Nura111Author Commented:
ok butI have a problem now:
when Im adding the <a href="http://mysite.com?desktop=1">Desktop Link</a>
It will work only to page by page  in a case when the user will go the full site with the link and than browse to a different page it will than go back to the mobile site
0
 
liveaspankajCommented:
not if you have read the cookie and changed layout on each page
0
 
Nura111Author Commented:
Ok Thank you I did it with the cookie and its fine
I dont know a lot about it if you have a good refrence I would love to see it.

but the problem with settiong the cookie it will stay in the desktop version unless the cookkie will be erased somehow right?
thats what happening when im testing it in firebug mobile amulator tool
0
 
liveaspankajCommented:
yes you need another button for that to remove it...or give another value
0
 
Nura111Author Commented:
I just did with SEssion than I think Its make more sense
0
 
Nura111Author Commented:
Oh I think its the same issue with SESSION as well :)(
0
 
Nura111Author Commented:
Is there a way to reset the COOKKIe or Session when the session(the user closed the website)
Is done?
0
 
liveaspankajCommented:
session is automatically terminated when browser is closed...
cookie you can decide...
check the php.net site you will get all details
0
 
ob2sCommented:
Hi,
Here's an example you can experiment with.  See if this is the behavior you want.  Click on the "Mobile" link to set the "mobile" preference and click on the "Desktop" link to set the desktop preference.  Then when you click on the "Default" link, you'll see the the preference is preserved-- the word "mobile" or "desktop" is displayed, depending the value returned by the want_mobile() function, and the desktop=x param doesn't appear in the location bar of the browser.  This example doesn't set the expiration on the cookie call, so cookie will expire at the end of the session.

You'll need to add the browser ident test as you see fit, but then your script can call want_mobile() to determine if the mobile or the desktop site should be displayed.  In this example, the GET param has highest precedence, then the client cookie value (if set), then the lowest priority is the browser ident (set to false for this example).
<?php
function want_mobile()
{
	if(isset($_GET['desktop']))
	{
		$want_mobile = empty($_GET['desktop']) ? 0 : 1;
	}
	elseif (isset($_COOKIE['desktop']))
	{
		$want_mobile = empty($_COOKIE['desktop']) ? 0 : 1;
	}
	else
	{
		if ( 0 /* replace with browser ident check for mobile devices */ )
		{
			$want_mobile = 1;
		}
	}
	if (isset($want_mobile))
	{
		# set cookie for next load, defaults to expire at end of session
		setcookie('desktop', $want_mobile);
	}
	return $want_mobile;
}

# reset cookie if requested by user
if (!empty($_GET['reset']))
{
	unset($_COOKIE['desktop']);
	setcookie('desktop', NULL, time()-100);
}

# decide which site to display
if (want_mobile())
{
	echo "<pre>mobile</pre>\n";
}
else
{
	echo "<pre>desktop</pre>\n";
}

echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '">Default Link</a><br /></br />';
echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '?desktop=1">Mobile Link</a><br />';
echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '?desktop=0">Desktop Link</a><br />';
echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '?reset=1">Reset Cookie</a><br />';
?>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 11
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now