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)
Related
working on a small project and having a small problem. i am querying mysql, i am able to get the array and everything works well but i am adding a additional condition if returns 0 or id does not match the query then redirect.
This is what i have so far.
$q = "SELECT TEST.*,
EMPLOYEE.EMP_ID
FROM TEST
LEFT JOIN EMPLOYEE ON TEST.EMP_ID = EMPLOYEE.EMP_ID
WHERE ITEM_ID=".$db->qstr($item_id);
if(!$item_id = $db->execute($q)){
force_page('system', 'Nothing found');
exit;
} else {
$view = $item_id->GetArray();
}
return $view;
}
so the above returns the array, query grabs $item_id from the url so it could be anything. i would like to check if that id exists if not then force the page as shown in the above. any suggestion on what i am doing wrong?
If you want to redirect you can use header function
header("Location: put_your_url_here");
e.g.
header('Location: /mypage.php');
There is no function such as force_page in PHP. You can use header to redirect user to another page. Modify your if condition as
if(!$item_id = $db->execute($q)){
header("Location: url_to_redict");
die();
}
header("Location: Your_url"); will help you to redirect based on your condition.but make sure that you have not echoed any thing before the header otherwise it won't redirect.
I don't think there is any function like force_page(); in php
You need to redirect like header("Location: Your_url");
And If you have function force_page(); please post code of that.
I am redirecting to a different page with Querystring, say
header('location:abc.php?var=1');
I am able to display a message on the redirected page with the help of querystring value by using the following code, say
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
echo 'Done';
}
}
But my problem is that the message keeps on displaying even on refreshing the page. Thus I want that the message should get removed on page refresh i.e. the value or the querystring should not exist in the url on refresh.
Thanks in advance.
You cannot "remove a query parameter on refresh". "Refresh" means the browser requests the same URL again, there's no specific event that is triggered on a refresh that would let you distinguish it from a regular page request.
Therefore, the only option to get rid of the query parameter is to redirect to a different URL after the message has been displayed. Say, using Javascript you redirect to a different page after 10 seconds or so. This significantly changes the user experience though and doesn't really solve the problem.
Option two is to save the message in a server-side session and display it once. E.g., something like:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
This can cause confusion with parallel requests though, but is mostly negligible.
Option three would be a combination of both: you save the message in the session with some unique token, then pass that token in the URL, then display the message once. E.g.:
if (isset($_GET['message'], $_SESSION['messages'][$_GET['message']])) {
echo $_SESSION['messages'][$_GET['message']];
unset($_SESSION['messages'][$_GET['message']]);
}
Better use a session instead
Assign the value to a session var
$_SESSION['whatever'] = 1;
On the next page, use it and later unset it
if(isset($_SESSION['whatever']) && $_SESSION['whatever'] == 1) {
//Do whatever you want to do here
unset($_SESSION['whatever']); //And at the end you can unset the var
}
This will be a safer alternative as it will save you from sanitizing the get value and also the value will be hidden from the users
There's an elegant JavaScript solution. If the browser supports history.replaceState (http://caniuse.com/#feat=history) you can simply call window.history.replaceState(Object, Title, URL) and replace the current entry in the browser history with a clean URL. The querystring will no longer be used on either refresh or back/previous buttons.
When the message prompt ask for a non exsisting session. If false, show the message, if true, do nothing. session_start(); is only needed, if there is no one startet before.
session_start();
if ($_GET['var']==1 && !isset($_SESSION['message_shown']))
{
$_SESSION['message_shown'] = 1;
echo 'Done';
}
Try this way [Using Sessions]
<?php
//abc.php
session_start();
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
if(isset($_SESSION['views']))
{
//$_SESSION['views']=1;
}
else
{
echo 'Done';
$_SESSION['views']=1;
}
}
}
?>
Think the question mean something like this?
$uri_req = trim($_SERVER['REQUEST_URI']);
if(!empty($_SERVER['REQUEST_URI'])){
$new_uri_req = str_replace('?avar=1', '?', $uri_req);
$new_uri_req = str_replace('&avar=1', '', $new_uri_req);
$pos = strpos($new_uri_req, '?&');
if ($pos !== false) {
$new_uri_req = str_replace('?&', '?', $new_uri_req);
}
}
if( strrchr($new_uri_req, "?") == '?' ){
$new_uri_req = substr($new_uri_req, 0, -1);
}
echo $new_uri_req; exit;
You can use then the url to redirect without vars. You can also do the same in js.
str_replace() can pass array of values to be replaced. First two calls to str_replace() can be unified, and filled with as many vars you like that needs to be removed. Also note that with preg_replace() you can use regexp that can so manage any passed var which value may change. Cheers!
I'm new to PHP, and just creating a simple website.
At the moment, I have a header with some links (i.e. blog, faq, home, gaming etc). I'm trying to use GET functions to show new content in a container on the webpage. I've tried having them link to the index and to a specific page, like
Home
and then having some PHP in the html body...
<?php
if ($_GET[page] == "faq") {
$result === 'FAQ';
} else {
$result === 'Non-FAQ';
}
echo $result;
?>
just to see if it would work, and lo and behold, it doesn't.
So, that's basically the gist of what's happening. It's baffled me for the past few hours, and would really appreciate some help
Thanks
You aren't using the assignment operator to assign a value to $result. Use a single equals sign, ie
<?php
if ($_GET['page'] == "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
<?php
if ($_GET[page] === "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
If you want to DEFINE a string you use one "=", if you want to compare it you use "===" (or ==) :)
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.
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)