PHP parameter without value using for navigation - php

I am trying to set up a navigation system that uses GET parameters with no value, here's an example:
http://foo.bar/?mainPage
takes the visitor to the main page.
So I am using if statements and I am wondering if there is a way I can do this without a bunch of if statements? I don't think switch statements would work for this. This is currently what I have:
$mainPage = $_GET['mainPage'];
$contact = $_GET['contact'];
if(isset($mainPage)) {
// go to main page
}
if(isset($contact)) {
// go to contact page
}
I don't want to have to end up writing a ton of if statements, though. Any ideas?
Thanks.

You could use $_SERVER['QUERY_STRING'], which gives everything in the URL after the question mark, then just use a switch statement to choose which page.

I'd suggest using an associative array:
$location_map = array(
'mainPage' => 'some-url-here',
'contact' => 'some-other-url-here'
);
foreach($location_map as $key => $value)
{
if(array_key_exists($key, $_GET))
{
header('Location: ' . $value);
exit();
}
}
//if we reached here, then redirect to home page

You can use a header redirect like so:
<?php
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>

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)

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

php reload page with updated query string

let say I have an url like this
http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj
Now i want to update p2=100 and reload the page using php
here parameters can be unlimited (p1,p2,...pn), and we can update any param and reload the page.
Fairly simply, you can do this
$_GET['p2'] = 100;
header("Location: http://www.domain.com" . $_SERVER['REDIRECT_URI'] . '?' . http_build_query($_GET));
The question is kind of vague, but assuming you want to reload from the client side using javascript:
window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
Reload your page you just have to setup your variables the way you want it in the URL field
If you want to reload page with desired parameters use JS
Following script might help you
window.location = "http://www.domain.com/myscript.php?p1=xyz&p2=100&p3=ghj"
window.location = "http://www.domain.com/myscript.php?p2=200&p1=dfgb&p3=asdhahskh&etc=alotofparameters"
Now if you want to reload the page after a specific amount of time interval then you can use the following meta tag
<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">
Njoy Coding.
:)
Here is what I use when I want to change 1 $var value and then redirect.
function getUrlWithout($getNames){
$url = $_SERVER['REQUEST_URI'];
$questionMarkExp = explode("?", $url);
$urlArray = explode("&", $questionMarkExp[1]);
$retUrl=$questionMarkExp[0];
$retGet="";
$found=array();
foreach($getNames as $id => $name){
foreach ($urlArray as $key=>$value){
if(isset($_GET[$name]) && $value==$name."=".$_GET[$name])
unset($urlArray[$key]);
}
}
$urlArray = array_values($urlArray);
foreach ($urlArray as $key => $value){
if($key<sizeof($urlArray) && $retGet!=="")
$retGet.="&";
$retGet.=$value;
}
return $retUrl."?".$retGet;
}
This takes the url ($_SERVER['REQUEST_URI']), removes the desired values ($getNames) [which can be one or more values], and rebuilds the url. It can be used like-
$newurl = getUrlWithout(array("p2"));
header( 'Location: http://www.domain.com/'.$newurl.'&p2=100' );
Try below codes:
$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = preg_replace('/p2=([0-9]*)&/', 'p2=100&', $varURL);
header('location:'.$varNEwURL);
OR
$varURL = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$varNEwURL = $varURL.'&p2=100';
header('location:'.$varNEwURL);

php header instant redirect if specified campaign urls are visited

I might worrying way too much here, but here it goes...
I have set up google analytic campaign tracking URLs using the google campaign link builder.
Which is great, but I have like 20 different tracking urls, and more to come.
These URLs are really ugly and I'm not a fan of the visitor seeing this long tracking url on there first visit to my site.
So this is my idea/theory that I want to put on my wordpress site. If anyone would be kind enough to help with the php writing part of it, I would be very grateful. Or any advice if the idea is a bad one.
For example, these are just some of my URL's
http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=Promo
http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=Promo
http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=Promo
I would like to instantly redirect all of the above urls too...
http://example.com/
Using php in my header.php or functions.php
Is there some how this can be written so I can simply add new tracking urls in an array/case perhaps.
Any suggestions would be great thanks!
Please don't laugh as this - but at a guess this is what I'm trying to do...
$landing = $_SERVER['REQUEST_URI'];
$campaigns = array(
"http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=Promo"
);
if ( $campaigns == $landing ) {
header( 'Location: http://example.com/' ) ;
}
Try this,
this is pure javascript to clear utm_params from url after load the page.
function clear_utm_from_url(){
var currentLocation = window.location.search.replace(/\?/g, '').split('&');
var new_url = new Array();
for( i = 0; i < currentLocation.length; i++){
var q = currentLocation[i].split('=');
if( q[0].search("utm_") ){
new_url.push(currentLocation[i]);
}
}
if( new_url.length > 0 ){
new_url = "?"+new_url.join("&");
}else{
new_url = "";
}
history.pushState({}, "", window.location.pathname+new_url);
}
And add something like this, or others to run function
<body onload="clear_utm_from_url();">
You should not do that.
Google analytics tracking code is client side. It is a piece of js code which executes once the page is loaded. If it does not find utm_* query parameters, nothing is going to be sent to ga, so the stats you are going to see there won't be readable. Most probably you will just see the grand total in the ga ui.
If you want to do it anyway...
$trackingParams = array('utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term');
$queryParams = array();
parse_str($_SERVER['QUERY_STRING'], $queryParams);
if (count(array_intersect($queryParams, $trackingParams)) > 1) {
// This url has tracking params
foreach ($trackingParams as $paramName) {
unset($queryParams[$paramName]);
}
// You can use a regex, but i wouldn't recommend it
$newUrl = str_replace(
$_SERVER['QUERY_STRING'],
http_build_query($queryParams),
$_SERVER['REQUEST_URI']
);
header('Location: ' . $newUrl, true, 301);
exit;
}
This is code is going to search for utm_* params in current url and redirect to an url without them, leaving other query params intact.
A simple method for removing the query string:
if ($urlIsTracker) {
list($url) = explode('?', $_SERVER['REQUEST_URI']);
header("Location: $url", true, 301);
exit;
}

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.

Categories