php redirect based on url variable - php

I want to create a URL redirect based on a URL variable.
so, if student%20gender (student gender) is male then go to www.one.com, if female, go to www.two.com.
couldn't figure this one out yet. any help?

Question could use a little bit of a better explanation. Do you mean that someone is going to
http://www.yoursite.com/yourscript.php?student%20gender=male and you want them to be redirected to http://www.one.com?
If this is the case, PHP has a built in variable known as $_GET which stores the values listed after a ? in a URL. So in the above example, we'd see:
$_GET['student gender'] = male;
You can use this to access any number of parameters separated by &
So the URL http://www.site.com/index.php?val1=a&val2=b&val3=c would give us:
$_GET['val1'] = a;
$_GET['val2'] = b;
$_GET['val3'] = c;
After this, to do a redirect in PHP the easiest way is to send a Location: header. This is done like so:
<?php
header("Location: www.newsite.com");
?>
Combining this with our $_GET variable and some simple logic:
<?php
if($_GET['student gender'] == 'male'){
header("Location: www.one.com");
die();
} else {
header("Location: www.two.com");
die();
}
?>

$var = $_GET['yourvar'];
if($var == 'one'){
header("Location: http://www.one.com/");
}else if ($var == 'two'){
header("Location: http://www.two.com/");
}
then do http://www.yoururl.com?yourvar=one

You also have to make sure you look at the security aspects here, the best way yo accomplish this is
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : false;
switch($gender)
{
default: //The default action
//Send back to the gender select form
break;
case 'male':
//Send to male site!
break;
case 'female':
//Send to female site!
break;
}
This should be sufficient, but please never use $_X['?'] in functions that execute either shell or database queries without sanitation.
Note: _X being (GET,POST,REQUEST,FILES)

Related

Creating dynamic header links in PHP

In my code i have an if statement, which is return is true, it would header the user to a specific page. But i want to send some data to the next page. So i tried using dynamic links. but it doesn't seem to work. Here's my code;
<?php
$row = mysqli_fetch_object($query);
if($row->Usertype = "General_User")
{
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid= echo $row->Company_ID");
}
else
{
header('Location: http://www.mywebsite.com');
}
?>
but when i'm redirected to the page, i get this;
http://www.mywebsite.com/GeneralUserHome.php?cid=%20echo%20'';
any suggestions?
Why do you have echo in there?
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid={$row->Company_ID}");
You have a typo here:
if($row->Usertype = "General_User")
That's an assignment, and will always be true. You want double-equals for comparison:
if($row->Usertype == "General_User")
As a note, I reverse the two in order to avoid these typos. This will error out and tell you exactly what was wrong, if you typo'd a single equals sign:
if("General_User" = $row->Usertype)

PHP GET Function as NULL

I want to use GET in PHP and I know how to. The only problem is when I start my page I dont have a ?var=... on it. For example my site is
http://localhost/q/question.php
and in my code is
$num = $_GET['num'];
And I want to assume that if $num == NULL or has not been defined as in the link above.
I will generate a different page. because as example below just determines I am starting the questions in what ever page.
No num variable = Start Generating questions in array.
http://localhost/q/question.php?num=1
if(isset($_GET['num')){
header('Location: someotherpage.php'); //redirects user to someotherpage.php
//do something
}
else{
//do something else
}
<?php
if(isset($_GET['num'))
{
echo "is not null";
}
else
{
echo "is null";
}
?>

Using PHP to identify current page

I have a series of PHP page, and I would like to use conditional logic to apply different rules to each page. Im not sure if my method is the best way to go about it, so I wanted to see if the community had any recommendations, as this doesn't feel like the best approach. Code Below:
<?php
$nameurl = $_SERVER["REQUEST_URI"];
if ($nameurl == "/fs/about.php"){
echo "about page";
}
elseif ($nameurl == "/fs/index.php"){
echo "home page";
}
?>
Ideally, I would like to only use the filename (index.php or about.php) instead of having /fs/. Im not sure if there is another way of using $_SERVER with PHP but it seems like there might be a more efficient and reusable way of writing this. Thoughts?
You could use
// get script name
$script = explode('/', $_SERVER['PHP_SELF']);
$scriptname = $script[count($script) - 1];
switch ($scriptname) {
case "index.php":
// Something you only want to show on this page
break;
case "about.php":
// Something you only want to show on this page
break;
}
To save a couple of lines of code, you could replace the multiple ifs with a switch:
http://php.net/manual/en/control-structures.switch.php
$nameurl = $_SERVER["REQUEST_URI"];
switch ($nameurl) {
case "/fs/about.php":
echo "about page";
break;
case "/fs/index.php":
echo "home page";
break;
default:
echo "unknown page";
break;
}
Makes it a little easier to add new cases in the future, but it's essentially doing the same thing...
There might be ways to make it more optimized, but I think if you start doing too much you lose the ability to easily understand what's happening in the code, so unless you comment what you're doing future people looking at your work will curse you. :P
Try this
$nameurl = basename($_SERVER['REQUEST_URI'], '.php');
echo $nameurl, " page";
http://php.net/manual/en/function.basename.php
You could try :
$currentFile = $_SERVER["PHP_SELF"];
$current_filename = explode('/', $currentFile);
or
$current_filename = basename($_SERVER['REQUEST_URI']) .'php';

Code Help - PHP, Query String, Redirect

I'm trying to pass variables to a simple PHP script and have it redirect to different URLs depending on the values in the query string.
Here's what I have in bonus.php:
<?php
if ($_GET['pid'] == '3') {
$bonus = "copy-paste-traffic";
}
elseif ($_GET['pid'] == '5') {
$bonus = "lazy-affiliate-riches";
}
$redirect = "http://affiliatesilverbullet.com/.'$bonus'.-bonus/?mid=.'$_GET['mid']'.&pid=.'$_GET['pid']'.";
echo $redirect;
page_redirect($redirect);
?>
I want queries to redirect as follows:
asbfree.com/bonus.php?mid=dstruckman&pid=3 -> affiliatesilverbullet.com/copy-paste-traffic-bonus/?mid=dstruckman&pid=3
asbfree.com/bonus.php?mid=dstruckman&pid=5 -> affiliatesilverbullet.com/lazy-affiliate-riches-bonus/?mid=dstruckman&pid=3
But it's not working.
What am I doing wrong?
Please show me how to fix my bonus.php script to make this work.
Thanks in advance!
Dustin
I think you may change
$redirect = "http://affiliatesilverbullet.com/.'$bonus'.-bonus/?mid=.'$_GET['mid']'.&pid=.'$_GET['pid']'.";
to
$redirect = "http://affiliatesilverbullet.com/".$bonus."-bonus/?mid=".$_GET['mid']."&pid=".$_GET['pid'];
EDIT : ...change elseif to else if, page_redirect to http_redirect, and remove echo or place after redirect function.
I would use header("Location: $redirect"); instead of page_redirect($redirect);.
One of the issues may be your variable interpolation.
Replace
$redirect = "http://affiliatesilverbullet.com/.'$bonus'.-bonus/?mid=.'$_GET['mid']'.&pid=.'$_GET['pid']'.";
with
$redirect = "http://affiliatesilverbullet.com/{$bonus}-bonus/?mid={$_GET['mid']}&pid={$_GET['pid']}";
Next time you post a question it would be helpful to post the error message you are receiving.

Help with PHP if / else statement

On my site, forms are brought in via AJAX and checked against a sessionid. I know this is not optimal, but it's working for us. If the referrer doesn't have the session ID they are redirected back to "anotherpage". I need to allow some outside URL's access the form directly.
we set the sessionid on the page with the link to the form.
Here is what we have now on the form page:
<?php
$code = $_GET['sessionid'];
if(strcmp( $code , 'XXXXX' ) != 0) {
header("Location: http://www.domain.com/anotherpage.php");
}
?>
I need to allow some outside domains direct access to the form page and am having issues with this:
(I'm putting it above the head tag on the form page)
<?php
$code = $_GET['sessionid'];
$referrer = $_SERVER['HTTP_REFERER'];
if(strcmp( $code , 'XXXXX' ) !=0) {
header("Location: http://www.domain.com/anotherpage.php");
} else {
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
}
}
?>
this still bounces me back to "anotherpage.php" any ideas?
********EDIT*******
thx for the help, it works ad I requested. Now I see what I asked wasn't entirely correct. This appends the URL with =sessionid?=XXXXX. This isn't an issue on my site because I'm loading the content with .jquery .load so the URL doesn't change. I don't want the sessionid to be visible, and now it is. Can I either a) "trim" the url somehow or b) separate the two functions so they are exclusive?
if(strcmp( $code , 'XXXXX' ) !=0) {
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
} else {
header("Location: http://www.domain.com/anotherpage.php");
}
}
As I read your post, you want anyone from the preg_match to get the desired page regardless of sessionID status, so you don't want to test sessionID first.
Start the if block with the preg_match test.
Your first if is checking to see if they don't have the $code and redirecting them. This will always be the case. You should probably check the $referrer first and then do the $code check.
Try reverse if with else
<?php
$code = $_GET['sessionid'];
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/", $referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
} else if (strcmp( $code , 'XXXXX' ) != 0) {
header("Location: http://www.domain.com/anotherpage.php");
}
?>
If I'm not misunderstanding this, the problem is in the order in which you are checking things.
If you want to allow some referrers to access the site even if they don't have the session id, you have to check for that before checking for the session id. Otherwise, they will end up being treated just like everyone else.
You can either switch the order of the conditions (first check for the referrer and then check fo the session id) or check for the referrer inside the branch in which you already know the session id is not valid.
The issue could be in your regex, it should be:
if (preg_match("/site1\.com/",$referrer))
notice escaping the dot (.)

Categories