I'm trying to make redirect if the user is not signed in.
so it should read like this:
If FName is unset and $page does not equal 'new host' or 'login'
then set header to login and set error message.
All the parts work on their own, but not when I try to assemble them.
Edit, I have it working with a switch now but I'm still intrigued on why this if didn't work.
The || means true if either condition is true.
The and checks to see if the leading and following conditions are true.
Should I be using && or does that make a difference?
Here is my code:
if ( !isset($_SESSION['FName']) and
( $page == '/e-Party/Login/Login.php' || $page == '/e-Party/NewHost/NewHost.php')){
echo "not logged in";
$_SESSION['Error'] =
"you must login to use our site,<br>. Or create a account if you don't have one";
header( 'Location: /e-Party/Login/Login.php' );
exit();
}
Edit, here is my working switch.
if ( !isset($_SESSION['FName']))
switch ($page) {
case '/e-Party/NewHost/NewHost.php':
break;
case '/e-Party/Login/Login.php':
break;
default:
$_SESSION['Error'] =
"you must login to use our site,<br>. Or creat a account if you dont have one";
header('Location: /e-Party/Login/Login.php');
exit();
}
A comment helps much more than a down vote, thank you for reading.
Your if and switch are not equivalent: the switch version would be equivalent to
if (!isset($_SESSION['FName']) && $page != '/e-Party/Login/Login.php' && $page != '/e-Party/NewHost/NewHost.php') {
...
}
I changed the if to a switch and it works perfectly as I intended.
if ( !isset($_SESSION['FName']))
switch ($page) {
case '/e-Party/NewHost/NewHost.php':
break;
case '/e-Party/Login/Login.php':
break;
default:
$_SESSION['Error'] =
"you must login to use our site,<br>. Or creat a account if you dont have one";
header('Location: /e-Party/Login/Login.php');
exit();
}
You wrote
If FName is unset and $page does not equal 'new host' or 'login' ...
But you wrote (in PHP)
if !isset($_SESSION['FName']) and
($page == '/e-Party/Login/Login.php' || $page == '/e-Party/NewHost/NewHost.php')
That is, page equals A or page equals B. To make PHP match English:
if !isset($_SESSION['FName']) and
($page != '/e-Party/Login/Login.php' || $page != '/e-Party/NewHost/NewHost.php')
which is flawed. It is equivalent to
if !isset($_SESSION['FName'])
Related
I am running an argument based on urls to determine whether a user can access that area or not of the site. Is there a better way to instead of listing our every url just say anything after / cant be access. Here is my code it will better explain the problem:
if( $siteUrl == '/shop-portal/' && $userRole == 'customer'){
header('Location: http://mywebsite.co.uk/');
} elseif( $siteUrl == '/product-category/...' && $userRole == 'customer'){
header('Location: http://mywebsite.co.uk/');
}
so where I have /product-category/ isa there a way instead of writing out '/product-category/category1 etc etc every time can I just create one argument? Like anything after '/product-category/...' gets redirected?
I figured it out, here if any one else needs to see it:
if(strpos($siteUrl, 'shop-portal') !== false && $userRole == 'customer'){
header('Location: http://mywebsite.co.uk/');
} elseif(strpos($siteUrl, 'product-category') !== false && $userRole == 'customer'){
header('Location: http://mywebsite.co.uk/');
}
I changed up the query to check if the string exists in the url!
I'm trying to add some code to my website, in order to redirect users depending on their IP address. Whatever I do I'm getting a "This webpage has a redirect loop" message.
Here's my code:
if(isset($_GET['FirstTimer'])){
setcookie('FirstTimer','something',strtotime('+1 year'),'/');
$_COOKIE['FirstTimer']='something';
}
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
if(!isset($_COOKIE['FirstTimer'])):
if($_SERVER['REQUEST_URI'] == '/'){
switch((string)$country) {
case 'GR':
header('Location: http://mywebsite.com/');
break;
case 'RU':
header('Location: http://mywebsite.com/ru');
break;
default:
header('Location: http://mywebsite.com/en');
}
}
endif;
geoip_close($gi);
The "FirstTimer" cookie is used to determine whether the visitor is a new one or not (because I'd like to redirect new visitors only).
The code is placed on the top of my index.php file, and there are no spaces before '\<\?php ?>' tags.
I am assuming that $_GET['FirstTimer'] is not set. So your switch is always executed.
When geoip returns GR your script will redirect to http://mywebsite.com/ again.
$_SERVER['REQUEST_URI'] returns / and you are back in your switch again.
There is your loop.
EDIT
I've changed your script a little bit. Instead of logging if the user visits the first time, which makes no sense, just log when the user visited the last time.
If this cookie is not set, start geoip and redirect the user. If it is set, update the time.
if(!isset($_COOKIE['last_visit'])) {
setcookie('last_visit',date("Y-m-d H:i:s"),strtotime('+1 year'),'/');
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
if($_SERVER['REQUEST_URI'] == '/'){
switch((string)$country) {
case 'GR':
header('Location: http://mywebsite.com/');
break;
case 'RU':
header('Location: http://mywebsite.com/ru');
break;
default:
header('Location: http://mywebsite.com/en');
}
}
geoip_close($gi);
} else {
setcookie('last_visit',date("Y-m-d H:i:s"),strtotime('+1 year'),'/');
}
But you should definitely enhance this script because what happens, if you have a user that gets on your page with this: http://yourwebsite.com/great/article/link.html.
Perhaps you do have a translation for this, but the user won't be redirected.
I have
$CID = $_REQUEST ['cid'];
$UID = $_REQUEST ['uid'];
if ($UID == '0') {
header ( 'Location: url/you-need-to-log-in-before-redirect/' );
} else {
switch ($CID) {
// go to URL1 case number is the same as CID
case "147" :
header ( 'Location: url' . $UID );
break;
case "148" :
header ( 'Location: url' . $UID );
break;
default :
echo "Something went terribly wrong";
}
}
Is this ok? I have a problem of $UID registering as blank which should not be possible as it is always set to be 0 for non-registered users and for registered it's unique. should i use exit() instead of break? or both? Or should i not be using header loaction at all to send users forward...i have hundreds of cases inside this switch case statement. i cant post any actual urls so the 'url' in example is not a variable but just a placeholder for the actuall full address.
Is the zero's type integer? If yes your condition is wrong, because it tests for string but you have an integer.
Try: if ($UID == 0) { and the same for the switch cases.
I have a main page that users go to that shows output from a MySQL query depending on the variable passed to it. So;
http://website/mypage.php?page=0
However, I would like to set up redirection so that if someone just goes to
http://website/mypage.php
that it will go to http://website/mypage.php?page=0. I thought of using the following code, which verifies the current page as well as verifies that a user's session is established;
elseif ($_SERVER['PHP_SELF'] == '/mypage.php' && isset($_SESSION['valid_user']))
{
header('Refresh: 0; URL=/mypage.php?page=0');
}
But, this looks to be too general. Is there a way to check for exactly '/mypage.php' or maybe '/mypage.php?page=' ? I thought of using strlen to check for only the 11 characters in /mypage.php, but I'm not sure that this is the most efficient way of doing it.
You can check to see if the variable page has a value and if its empty you can do the redirect
if($_GET['page'] == ''){
header('Location: /mypage.php?page=0');
exit;
}
or
if(!isset($_GET['page'])){
header('Location: /mypage.php?page=0');
exit;
}
In your mypage.php you can check something like this
if(!isset($_GET['page'])) {
header('location: /mypage.php?page=0');
exit;
}
But, I think, instead of redirecting to same page with a get variable, why don't just show the page you want to show by default, when there is no page variable is set.
You should use this way:
header('Location: /mypage.php?page=0');
exit;
Otherwise check the $_SERVER variables for more strict match. http://php.net/manual/en/reserved.variables.server.php
I think probably you need $_SERVER['REQUEST_URI']
Is this what you're looking for?
if(!isset($_GET['page']) || empty($_GET['page'])) {
Header('Location: http://website/mypage.php?page=0')
exit();
}
or
if(!isset($_GET['page']))
$_GET['page'] = 0;
I'm not sure about the second solution, you shouldn't attribute value to $_GET[] variables.
if( !isset($_REQUEST['page']) ) {
header('Location: /mypage.php?page=0');
exit(0);
} else {
if( $_REQUEST['page']=="" ) {
header('Location: /mypage.php?page=0');
exit(0);
}
}
Here is my code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">HANDHELD<br />THERMAL IMAGING</div>';
}
else if($_SERVER['REQUEST_URI'] == '/shop/category/mobile-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">MOBILE IMAGING</div>';
}
Basically this code displays a different left side site navigation depending on which page you're on in the site. It detects what page you're on by the URL of the page by using the PHP REQUEST_URI feature.
My question is this, how can I make the code detect multiple URLs for the same navigation piece?
I tried this code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/' , '/shop/iphone-thermal-imaging-adapter/')
{
But that code doesn't seem to work. Basically all I'm trying to figure out here is how I can use multiple URLs for the REQUEST_URI 'if' statement. Thanks in advance!
Put the URLs in an array and then...
if(in_array($_SERVER['REQUEST_URI'],$array))
You should... switch to a switch statement, which is both neater and offers this option:
switch($_SERVER['REQUEST_URI']) {
case '/shop/category/handheld-thermal-imaging/':
// THERE IS NO CODE AT ALL HERE!
// The absence of a "break;" causes control to "fall through" to the next
case '/shop/iphone-thermal-imaging-adapter/':
echo "something common for the thermal imaging urls";
break;
case 'some other url':
echo "something for the url that stands alone";
break;
}
You can use an "OR" statement in your conditional:
if($_SERVER['REQUEST_URI'] == "whatever" || $_SERVER['REQUEST_URI'] == 'something else")
{
}
I used the OR statement for multiple URL checking
<?php
if ((strpos($_SERVER['REQUEST_URI'], 'contact.php') || strpos($_SERVER['REQUEST_URI'], 'register.php') || strpos($_SERVER['REQUEST_URI'], 'login.php')) === false) {
echo "show";
} else {
echo "hide";
}
?>