I've built a mobile detection with Mobile_Detect.php and it works great. Now if a User does not want to stay on the mobile site he can click on "Desktop-Version" and goes back to the main page via a 'mobile_off.php' which sets $_SESSION['mobile'] = 'off'.
The main Site executes the following code:
<?php
session_start();
// Did the User come back from mobile.php?
if ($_SESSION['mobile'] != 'off') {
include 'Mobile_Detect/Mobile_Detect.php';
$detect = new Mobile_Detect();
// Smartphone?
if ($detect->isMobile() && !$detect->isTablet()) {
// Redirection --> echo 'JS'
echo "<script>window.location='mobile.php'</script>";
}
}
?>
The Problem seems to be that if ($_SESSION['mobile'] != 'off') is ignored or wrong. My iPhone always sends me straight back to 'mobile.php'.
Can anyone help?
Perhaps I should show you the code from 'mobile_off.php':
<?php
session_start();
$_SESSION['mobile'] = 'off';
?>
You need to first check the session variable exists or not before checking the value of session variable.
For checking if session variable is set or not try this code
if(isset($_SESSION['mobile'])
After that check the value of session variable
Related
This question already has answers here:
Transfer variables between PHP pages
(5 answers)
Closed 5 years ago.
Sorry, this is a bit of a noob question, but...
I am creating a login page and I am having difficulty getting the login page to send me back to the web page that prompted the login redirect...
Can the previous page be stored via POST variable and accessed on the next page (login.php)?...I am having trouble just keeping that webpage url...if someone could show me how and explain why that would be amazing!
If I could just see how it looks to store variables to post so they can be viewed on the next page that would solve all my problems
original webpage:
<?php
//how do I post current url so it can be accessed on login
require_once('./../User_Auth/includes/authenticate.php');
?>
login webpage:
`header("Location:");`
Use Sessions to pass data to next page!
<?php session_start(); ?> // session starts with the help of this function
<?php
if(isset($_SESSION['use'])) // Checking whether the session is already there or not if
// true then header redirect it to the home page directly
{
header("Location:home.php");
}
if(isset($_POST['login'])) // it checks whether the user clicked login button or not
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "Ank" && $pass == "1234") // username is set to "Ank" and Password
{ // is 1234 by default
$_SESSION['use']=$user;
echo '<script type="text/javascript"> window.open("home.php","_self");</script>'; // On Successful Login redirects to home.php
}
else
{
echo "invalid UserName or Password";
}
}
?>
What I've understood from your question is that you are on page that a certain point will redirect you to the login page, and after the login you would come back to the previous page, right?
If so:
From the php $_SERVER var you can get this information.
Practice example:
file loginpage.php
<?php
//at the beginning you place the code that catch the POST data if a login request was sent
if(!empty($_POST["username"]) && !empty($_POST["password"])){
//HERE DO LOGIN TRY
if(LOGIN_SUCCESSFULL){
$page = $_SERVER["HTTP_REFERER"]; //this contain the previous page
header("Location: $page");
}
else{
// show an error
}
}
else{
//if you arrive at this point of the code, this means that the we are visiting login page, so we have to rendere the page
require_once 'login_page_body_with_login_form.php'
}
Of course there are more advanced and secure technique, but this should give both an answer and an idea of how make things together.
Use this code on the landing page to redirect to the login page, passing the landing page URL as a $_GET variable.
if ( USER_NEEDS_TO_LOGIN ) {
$login = "/path/to/login/page";
$login = $login . '?tgturl=' . $_SERVER[ "REQUEST_URI" ];
header("Location: $login");
}
You could pass your previous page value from current page to authenticate.php.
<?php
$previousPage = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//how do I post current url so it can be accessed on login
require_once('./../User_Auth/includes/authenticate.php');
?>
You can then access $previousPage in authenticate.php.
I have a problem with the Mobile Detection Script.
There are two scenarios:
First the script should detect if it's a mobile or not. If mobile, than redirect to another page (this works fine).
The second query should determine, if the person is on the root page or not. If it's not the root page, the layout should be the classic one. (no redirection)
But when I add this line there won't be anymore redirection, even if I open the root page on a mobile.
I also tried to destroy the session on the google_mobile.php (redirected page) and set the $_SESSION['layoutType'] = 'mobile', but anyway the session is set to classic when I open the root page.
Thanks for your help!
Here is the script:
session_start();
require_once 'Mobile_Detect.php';
function layoutTypes() {
return array('classic', 'mobile');
}
function initLayoutType() {
// Safety check.
if (!class_exists('Mobile_Detect'))
return 'classic';
$detect = new Mobile_Detect;
$isMobile = $detect->isMobile();
$layoutTypes = layoutTypes();
// Set the layout type.
if (isset($_GET['layoutType'])) {
$layoutType = $_GET['layoutType'];
} else {
if (empty($_SESSION['layoutType'])) {
$layoutType = ($isMobile ? 'mobile' : 'classic');
} else {
$layoutType = $_SESSION['layoutType'];
}
//check if it's the root page
if ($_SERVER['REQUEST_URI'] != "/")
$layoutType = 'classic';
}
// Fallback. If everything fails choose classic layout.
if (!in_array($layoutType, $layoutTypes))
$layoutType = 'classic';
// Store the layout type for future use.
$_SESSION['layoutType'] = $layoutType;
return $layoutType;
}
$layoutType = initLayoutType();
if ($_SESSION['layoutType'] == 'mobile') {
header("Location: www.example.com/google_mobile.php");
exit;
}
I've tested your code, it seems to work as you described. I'd guess it is a session issue.
session_destroy() does not clear your previous session state in the immediate session. That means your $_SESSION would still be "dirty" in a script even if session_destroy() is the first line in it. It's safer to clear cookies from your browser instead.
One other possible problem would be query string. You're checking the REQUEST_URI and it includes any query string on URI. "/?foo=bar" is certainly not "/". You may want to check SCRIPT_NAME (i.e. $_SERVER['SCRIPT_NAME'] == 'index.php) instead.
The code below page keeps session on GET requests or refreshing browser, but when I submit a form the session data is lost.
$user=$_POST['user']; $pass=$_POST['pass'];
if ($_POST['user'])
{ if($user==$un and $pass=$pw)
{ $_SESSION['uid']=$Xid;header('Location: '.$uri.'?welcome'); }
else { $msg="chybny login"; }
}
if(isset($_GET['logout'])) { session_destroy(); header('Location: '.$uri); }
$cnt=$_SESSION['cnt']+1; $_SESSION['cnt']=$cnt;
Above is the code for login which re-directs me to the welcome page as it was verified, however the session is lost. If I just refresh or repeatedly load the page without submitting, the session holds by echoing the session variable cnt (counts up 1,2,3,...)
After submitting the form, I see session is lost and too cnt variable is reset?
I usually don't work with session directly try the following, place it a the top of your script :
session_start();
$uid = $_SESSION['uid'];
$cnt = $_SESSION['cnt'];
then work with the variable instead
The problem is likely your 'and' statement. It should be &&. The condition is not going to be true.
If you're 100% sure the code is all fine and the PHP.ini is the problem, based on your comments above. Look at this link at check the settings in the .ini http://php.net/manual/en/session.configuration.php
To pass the current session to the next page... I believe is what you are asking...
You are currently not passing the session to the next page and use session_start() at the top of the next page.
Change line 4 to:
{ $_SESSION['uid']=$Xid;header('Location: '.$uri.'?'.SID.'&page=welcome'); } // Where "page" is the name of the data you are retrieving
Or, you can save the session data to a cookie and then retrieve it on the next page.
You can alternately name the session when you use session_start("NameHere") on each page, however if the visitor has recently visited and the session not destroyed, they may see parse errors, if you have them enabled.
First of all, make sure that the the first thing you do on every page is to start a session (I recommend calling it once in a header file that you require on all of your sub sites).
So that you have session_start(); everywhere in the system.
Second of all, tighten up your code; make it easier to read. Something like
$userName = isset($_POST['userName']) ? $_POST['userName'] : false;
$password = isset($_POST['password']) ? $_POST['password'] : false;
$logout = isset($_POST['logout']) ? $_POST['logout'] : false;
$url = '../index.php';
if(!($logout))
{
if($userName && $password)
{
if($userName == $un && $password == $pw)
{
$_SESSION['loggedIn']=true;
$_SESSION['uid']=$Xid;
$_SESSION['message']="success";
}
else
{
$_SESSION['loggedIn']=false;
$_SESSION['message']="fail, incorrect login information.";
}
}
else
{
$_SESSION['loggedIn']=false;
$_SESSION['message']="fail ; username and password not submitted.";
}
header("Location: $url");
}
else
{
session_start();
session_destroy();
session_start();
header("Location: $url");
}
And if you want to display unqiue content depending on whether a user is logged in or not, then you can simply check if the login session is set or not, on each page, instead of modifying the header for that.
I'm having a problem with this really short piece of code which I cannot get to work.
Basically, I want to display two different messages, depending on whether the user is new or returning.
<?php
if(isset($_COOKIE['visit']) && ($_COOKIE['visit'] == "true"))
{
echo 'cookie set, welcome back';
}else{
echo 'cookie not set, welcome new user';
}
setcookie("visit", "true", time()+60*60*24*600);
?>
The problem is that the cookie is not being set. I don't know what is wrong, can anyone help me?
You need to move the setcookie into the condition:
<?php
if(isset($_COOKIE['visit']) && $_COOKIE['visit'] == "true"){
echo 'cookie set, welcome back';
}else{
echo 'cookie not set, welcome new user';
setcookie("visit", "true", time()+60*60*24*600);
}
?>
If its not working then you should check the cookie is being sent back to the script. eg your browser or some browser addon interfering.
Be aware there is a new law that if your server is based in the EU, it requires you to obtain informed consent from your visitors before you can store or retrieve any information on there computer. http://www.cookielaw.org/
I had used the above code to make a modal window show only once.
The problem with the above is I was trying to use it at the end of a index page you can only use setcookie at the point where headers are set.
Instead I used:
if(isset($_COOKIE['visit']) && $_COOKIE['visit'] == "true"){
$load = false;
} else {
$load = true;
setcookie("visit", "true", time()+60*60*24*600);
}
That sets the cookie at the top of my PHP file if its not set then I use
if($load == true){
//do code on first run
}
where I needed to add code for the single run, I used it in the head to set a css style sheet for the one time use and in the footer to set some script / html for the one time use.
I'm trying to create a very simple login in php. All i want to do is,
register a session called user if the login is successful and direct the user to an inner page. in that inner page i have a include file which should check if the user session is created or not
if created -> authorize user
if not created -> redirect to login again.
But still I couldnt get this up and running. below is my code
login.php
session_start();
global $user;
if (($_POST['Submit'])){
$login = $_POST['login'];
$password = $_POST['password'];
if ((do_login($login, encrypt_password($password))) > 0){
$_SESSION['user'] = $login;
header('Location: home/dashboard.php');
}
else{
// load login again
}
}
and in my dashboard.php page this is how I'm checking it (and this part i have in another file called 'authentication.inc')
<?php
session_start(); // If
if (!isset($_SESSION['user'])) {
// User is not logged in, so send user away.
header("Location:/login");
die();
}
?>
updated ::
when I do an echo $_SESSION['user'], I'm expecting to see login name ($login) of the user which i done get :C
Am I missing something here... thanks in advance
cheers
sameera
if (!isset ($_POST['Submit']) || $_POST['Submit'] != 'Login'){
The code inside that if block won't get run if the form is submitted properly, because that condition reads " if Submit isn't set or it isn't 'Login' ". Try flipping the logic of that condition, ie:
if (isset ($_POST['Submit']) && $_POST['Submit'] == 'Login'){
-> " if Submit is set and it is 'Login' "
The Location header takes an absolute URL, not a relative URL, of the form:
header("Location: http://www.example.com/login.php");
There is an example on the PHP Manual header() page that can help to create the absolute URL.
And what #Brian says regarding the logic of your IF expression.