i'm trying to put a piece of code into my login script. If the users account is a particular type i want it to redirect to another url.
This bit i know how to do. but i want the url window to open in a pretty photo box which i have on my site. It's a kind of css box iframe window. I have the majority of my links opening in them by using this code:
Link
But when i try and do that for my redirect function it won't work. Can anyone let me know why this would be, i am typing the code like this:
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
<?php
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type))
if ($acctype['account_type'] == 'free_member') {
redirect_to("chatboard.php?iframe=true&height=260\" rel=\"prettyPhoto[1]\"");
}
?>
Ok so a couple things and I am not sure how you have it setup, but this is how it should be
<?php
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type)){
if ($acctype['account_type'] == 'free_member') {
redirect_to('chatboard.php?iframe=true&height=260&rel=' . $prettyPhoto[1]);
}
}
?>
And on your chatboard.php check for the proper variables making sure they are cleaned from possible attacks as well.
Related
In OpenCart 2, I am editing the appearance/php of the header only in the "success"/"thank you" page (catalog/view/theme/*/template/common/success.tpl).
So, in catalog/view/theme/*/template/common/header.tpl I want to do something like:
if( $is_thank_you_page ){
echo "stuff";
// bonus: I wanted to get the order email but maybe it should be a different post
}
But how can I check in the header.tpl if it is the "success"/"thank you" page?
I tried setting a variable in success.tpl before printing the header with no results.
You could try something like this (go about it based on your URL):
<?php
$parameters = explode('/', $_SERVER['REQUEST_URI']);
if(end($parameters) === 'success.tpl'){
//the condition with $parameters depends on the exact look of your URL
//you could also access an index directly
}
Basically, it takes the REQUEST_URI (part after the domain), splits it around the / symbols and then checks if it ends with success.tpl
You could also make a switch for the end($parameters) instead of the if.
I don't know opencart structure, but if this value never change you can try with strpos/stripos, something like:
if(stripos($var_with_page_title, 'thank you') !== false) {
do_something();
}
If you want you detect checkout/success page in you header, do following:
open catalog/controller/common/header.php
find
// Menu
$this->load->model('catalog/category');
Add before
// success page checking
$data['success'] = '';
if (isset($this->request->get['route']) && $this->request->get['route'] == 'checkout/success') {
$data['success'] = true;
}
// looking for email from the order
$data['success_email'] = '';
if ($this->customer->isLogged()) {
$data['success_email'] = $customer_info['email'];
} elseif (isset(this->session->data['guest']['email'])) {
$data['success_email'] = $this->session->data['guest']['email'];
}
Now in catalog/view/theme/YOUR_THEME/template/common/header.tpl
add anywhere you like
<?php if ($success) { ?>
//do something
<?php if ($success_email) { ?><?php echo $success_email; ?><?php } ?>
<?php } ?>
With bonus email
I have following code in the start of my php file but somehow it doesnt redirect to login page, however if I separate if condition into sub if condition then redirection works
Below code doesnt work
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) && $_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit;
}
///This doesnt work
Below code work
if (!isset($_SESSION['emailid'], $_SESSION['roleid'])) {
header('location:login.php?lmsg=true');
exit();
}
if ($_SESSION['roleid'] != 1) {
header('location:login.php?lmsg=true');
exit();
}
///this works
can someone help?
This is easier to read and it's probably want you meant to do:
if (!isset($_SESSION['emailid'], $_SESSION['roleid']) or (isset($_SESSION['roleid']) and $_SESSION['roleid'] != 1)) {
header('location:login.php?lmsg=true');
exit;
}
Total noob to PHP and I am trying to put this in the header of my wordpress file:
<?php
$meta = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $meta['geoplugin_countryCode'];
if (($country=='GB') or ($country=='US')) {
exit;
} else {
header("Location: http://google.com");
exit;
}
?>
Where the page code would get executed if from GB or US else get kicked to google.
Can't get it to work so guidance required please.
Cheerz
Ukphoneguy
You don't want to exit; in the "allowed" case.
$allowed = array('GB', 'US');
if (!in_array($country, $allowed)) {
// Not an allowed country. Redirect.
header("Location: http://google.com");
exit;
}
// The rest of your code for valid countries goes here
Your problems is that you were calling exit, even for GB and US. You only call exit when you want to completely stop executing any more PHP code (which is not what you wanted).
I would suggest:
if (($country != 'GB') and ($country != 'US')) header("Location: http://google.com");
i have been following an online tutorial on how to create a login and logout page in php and mysql,which seemed to be way easy not until i stage where i would like to modify the scripts,i am trying to put an admin's page where the user will have access to some links..i added a column in my users table called user_level,i want when a user logs in he his user_level is 1 he will access the links in the season.php script but if his user_level is 2 he will be directed to another page...i tried doing this below but its not working
if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}
this is my session.php code
<?php
if(!isset($_SESSION['name'])&&isset($_COOKIE['testsite'])){
$_SESSION['name'] = $_COOKIE['testsite'];
}
$dir = "profiles/".$_SESSION['name']."/images/";
$open = opendir($dir);
while(($file = readdir($open)) != FALSE){
if($file!="."&&$file!=".."&&$file!="Thumbs.db"){
echo "<img border='1' width='70' height='70' src='$dir/$file'>";
}
}
echo " <b>".$_SESSION['name']."</b>'s session<br /><a href='logout.php'>Logout</a>";
if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}
?>
Your syntax is wrong and you should not redirect if you want to show links, it should probably be something like:
if($user_level == 1){
// don't redirect here
include "links.php";
}
// check the php manual for the correct usage of if / else / elseif
elseif ($user_level == 2){
header("Location: client.php");
// terminate the script
exit;
}
Also, when using header("Location: client.php"); be sure that this is the first thing that comes out of your script. You can't dump out a whole html page and then put this at the bottom, it won't redirect.
When checking that variables passed via GET and POST are correct, I might have something like this:
<?php
//Controller
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(!isset($_POST['new_email']))
header('Location: somepage.php');
else if(empty($_POST['new_email']))
//Report error to user and prompt to try again
else
$newEmail = $_POST['new_email'];
if(!isset($_POST['full_name']))
header('Location: somepage.php');
else if(empty($_POST['full_name']))
//Report error to user and prompt to try again
else
$newName = $_POST['full_name'];
if(!isset($_POST['new_password_a']))
header('Location: somepage.php');
else if(empty($_POST['new_password_a']))
//Report error to user and prompt to try again
else
$newPasswordA = $_POST['new_password_a'];
if(!isset($_POST['new_password_b']))
header('Location: somepage.php');
else if(empty($_POST['new_password_b']))
//Report error to user and prompt to try again
else
$newPasswordB = $_POST['new_password_b'];
//Do some things with the variables
}
else
{
header('Location: somepage.php');
}
//View
//Display relevant view here
?>
How would you check GET and POST variables in your PHP script? I wonder if there is a better way?
Maybe creating a function to avoid the repeated code?
function check($varname,$destination,$message) {
if (!isset($_POST[$varname])) {
header("Location: $destination");
} else if (empty($_POST[$varname])) {
//Do something with $message
} else {
return $_POST[$varname];
}
return NULL;
}
And then,
<?php
//Controller
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$newEmail = check('new_email','somepage.php','Error message');
$newName = check('new_name','somepage.php','Error message');
$newPasswordA = check('new_password_a','somepage.php','Error message');
$newPasswordB = check('new_password_b','somepage.php','Error message');
//Do some things with the variables
//Checking for NULL values (although if some var was null,
//it should have either redirected or reported an error)
}
else
{
header('Location: somepage.php');
}
//View
//Display relevant view here
?>
What The Pixel Developer says is true though, you should sanitize the inputs at least against SQL injection (if you will use the data in a database) and CSRF attacks.
<?php
//Controller
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach ($_POST as $key => $value) {
if (empty($value)) {
echo 'whoops, remember to set ', $key;
} else {
switch($key) {
case 'new_password_a':
$newPasswordA = $value;
break;
//etc
}
}
}
if (isset($newPasswordA) && isset($newPasswordB)) { //check all vars have been set or whatever
header('Location: somepage.php');
} else {
header('Location: somepage.php');
}
Sorry I couldn't be more specific with the code, your sample code was kinda vague. I hope that helps.
Your code is a wild mess for a start. Please use brackets, better code comments and classes / functions.
You're not checking for anything correct other than if the key has a value. You might want to add a CSRF token to make sure the request has come from the form you are expecting.
Look at CSRF on Wikipedia.