Redirecting mobile-desktop versions manually - javascript

I have this code in my homepage
<?php
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
?>
and on the mobile version of my website, to get back to the destop version I inserted a link to this almost empty "redirect.php" page
<?php
$_SESSION['mobile']=0;
header("location: ../index.php");
exit;
?>
but I am not getting the desired behavior: once I enter the website with my mobile, I am correctly redirected to the mobile version, but once I click on the link to get the desktop version, I loop back into the mobile version. What am I missing?
Thanks! :)

Remember to call session_start() function before using sessions.
Better use this to reduce code:
<?php session_start();
if (isset($_GET["desktop"])) {
// DESKTOP
$_SESSION["mobile"] = 0;
...
} else {
// MOBILE
if (!isset($_SESSION["mobile"])) { $_SESSION["mobile"] = 1; }
if ($_SESSION["mobile"] == 1){ echo '<script>...</script>'; }
...
}
?>
And link to the homepage with ?desktop=1 to switch to the desktop version.

Based on your current code - You should have a separate session var that tell you that the user has manually reqeusted control.
<?php
if(!$_SESSION['manuall_overide'])
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
?>
<?php
$_SESSION['manuall_overide']=1;
header("location: ../index.php");
exit;
?>

Related

I need to do a site redirects to a page after 21.45 to 10.00

I need to do a site redirects to a page after 21.45 to 10.00
I need a script that would do a redirect site to another page at a certain time.
Here's what happened.
<?php
$date = new DateTime('now');
$date->setTimezone(new DateTimeZone('Europe/Athens'));
if ($date->format('H')>=10 && $date->format('H')<=21 ){
if ($date->format('H')==21 && $date->format('i')<45)
echo "<script>window.location = '/'</script>";
else if($date->format('H')<21)
echo "<script>window.location = '/'</script>";
}
?>
Not really sure with timezone, but
$hoursMinutes = intval(date("Hi"));
if($hoursMinutes >= 1000 && $hoursMinutes <=2145) {
header("Location: /2");
} else {
header("Location: /1");
}

Alert box pops up - redirect on click "OK"

I would like to redirect users as and when click "OK" in the alert box. Any hints will be appreciated
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message');</script>";
}
echo "<script type='text/javascript'>
alert('$message');
document.location.href='your url';
</script>";
You can use window.location.href to redirect to any page in javascript, so that it will be redirected to test.php after use click OK in alertbox:
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message');window.location.href='test.php';</script>";
}
echo "<script>alert('Update Not Successfully');document.location='pagename.php'</script>";
Using window.location.replace(url) is better, than window.location.href = url, because is disallows user to go back at this page using button "previous page in history" in browser.
It does a real redirect instead of just going to the next url:
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
$message = "Information already sent";
echo "<script type='text/javascript'>alert('$message'); window.location.replace('test.php'); </script>";
}

Javascript inside php not working

I've create a code that check if a variable is empty or not.
If the variable is empty I execute a javascript alert, in particular:
if($verbo_name == NULL)
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
If I insert a message inside the echo, the message appears correctly, but I want show the alert in javascript. What's the error? Thanks..
UPDATE more details:
$results = $con->query("SELECT verbo, descrizione FROM verbo WHERE verbo = '$verbo'");
$verbo_name = NULL;
while($row = $results->fetch_array())
{
$verbo_name = $row['verbo'];
}
Check below codes are working fine for me.
<?php
$verbo_name = NULL;
if(is_null($verbo_name))
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
?>
Check this one
if(empty($verbo_name))
{
echo "
<script>
alert('no record available in the database');
</script>";
exit();
}
This should trigger an alert:
<html>
<body>
<?php
if($verbo_name == NULL)
{
$msg = enter code here'no record available in the database';
} else {
$msg = 'not set';
}
echo "
<script>
alert('". $msg ."');
</script>";
?>
</body>
</html>
If the message is not set the problem is just the variable.
This is Pravat Kumar Sahoo's answer,
This must work fine.
If you are testing on google chrome, you might have prevented the alert dialog message mistakenly. Please try clearing the cache/cookie or test on any other browser.
<?php
$verbo_name = NULL;
if(is_null($verbo_name))
{
echo "<script>alert('no record available in the database');</script>";
exit();
}
?>
Update:
Instead of alerting the message inside PHP, echo a message, like "NoData". And in the ajax calling Javascript, receive response "success".
echo "NoData"; // In PHP file.
In your Javascript, get the response,
success:function(response)
{
if(response == "NoData");
{
alert("No data in the Database");
}
}

jquery script dosen't works in safari

I got problem with jquery code.It somehow doesn't work in safari browser. Rest of the browsers working fine but in safari I dont know why not any solution?
Index.php - I have this code inside HEAD
<script type="text/javascript">
$(document).ready(function () {
var prilohy = <? php echo json_encode($linkpar); ?> ;
if (prilohy == "Pizza") {
$('#content_donaska_extra').show();
} else if (prilohy == null) {
$('#content_donaska_extra').show();
} else {
$('#content_donaska_extra').hide();
}
});
</script>
Remove the space between <? and php
var prilohy = <?php echo json_encode($linkpar); ?> ;
script be
$(document).ready(function () {
var prilohy = <?php echo json_encode($linkpar); ?> ;
if (prilohy == "Pizza" || prilohy == null) {
$('#content_donaska_extra').show();
} else {
$('#content_donaska_extra').hide();
}
});

header by clicking button in JavaScript alert popup

Is there a way to direct to a desired page by clicking the OK button in a JavaScript pop-up?
I am able to achieve it when not within php but when echoed im not sure.
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo '</script>';
header("Location:login.php");
}
This headers OK but i would like it only to happen when the user clicks OK in the popup. Any suggestions?
Thanks
Just use this:
else {
?>
<script language="javascript">
alert("Wrong Username or Password");
location.href = "login.php";
</script>
<?php
}
Just use javascript to do this:
else {
var popup = window.confirm("Wrong Username or Password");
if (popup==true)
{
x="You pressed OK!";
window.location.href = "yourdomain.com/login.php";
}
else
{
x="You pressed Cancel!";
}
}
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo 'window.location = "/login.php";'
echo '</script>';
}
should work just fine. It won't redirect until OK is pressed.

Categories

Resources