Appending parameter to existing URL cause infinite loop - javascript

I have a page index.php, I want to append a parameter to the end of URL upon a certain action by user. So the result would be index.php?param=1
Using the below PHP code causing an infinite redirect loop.
<?php
header("Location: index.php?param=1");
?>
Using the below Javascript code result in infinite refresh loop.
window.location = "?param=1";
How can I append that parameter only once without encountering any loop using either PHP, Javascript or jQuery?

For the PHP version, try:
<?php
if( !isset( $_GET['param'] ) ){
header('location: index.php?param=1' );
}
?>

Please try add host name and then try.
Example - If you are using localhost.
<?php
header("Location: http://localhost/index.php?param=1");
?>

Apart from using Apache's mod_rewrite, you could check if the additional parameter is already set, e.g.
<?php
if (isset($_SERVER['QUERY_STRING']))
header("Location: index.php?param=1");
?>

It is better to use .htaccess files to set redirects due to there is the good practice to store logic and server configuration separately.
Here is .htaccess code for you:
Redirect /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
or if you need to have permanent redirect use
Redirect 301 /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
If you do not use Apache but Nginx or something else check their documentation how to make redirects. Typically it is very easy.

Related

Simple Page Redirect Using URL

I want my index.php to redirect to my welcome/index.html. I know there are so many ways to do it but I want to shorten it just like Facebook does. I want a url based redirection. I want to redirect index.php to welcome/index.html by using url.
For example:
example.com to example.com/?url=welcome
//this 'url=welcome' is the index.html of my welcome folder.
Here's the code, according to what I've understood. Place it on the very top of index.php. I'm assuming you're using the domain example.com
<?php
$destination = $_GET["url"];
switch($destination) {
case "welcome": // add your destinations here, one per single "case"
case "about":
case "anotherpage":
header("Location: /" . $destination . "/index.html");
break;
default:
echo "Error";
break;
}
?>
Doing this way you can manage which redirects will work and what not, avoinding "evil" usages of your redirect system.
Something malicious like example.com?url=http://evilsite.com will not redirect them to evilsite.com
This is probably not the best solution, but it's a good starting point to avoid not wanted redirections.
somewhere i read using header for redirection is little bit dangerous as header can b changed easily by hacker and hacker can send victim to another location easily. so i use javascript location.href function for redirection.
<?php
echo "<script>window.location ='http://example.com/welcome/index.html</script>";
?>
if you want to send user to dynamic url from example.com you can set a get variable like.
source address: http//example.com?url=http://example.com/welcome.html
destination address: http://example.com/welcome.html
<?php
if(isset($_GET['url']))
echo "<script>window.location ='$_GET[url]'</script>";
else
echo "<script>window.location='http://somewhereelse.com'";
?>
now u can dynamically set url variable in your source address and redirect user wherever you want to
Not really sure if this would help, but you're welcome to try
<?php
if (isset($_GET["url"])) {
$url = $_GET['url'];
header('Location:'.$url);
}
?>
I haven't tried this, but this is what I thought after seeing your post

Header location is not working on live server

I Have one registration and payment page. program flow is like registration > confirmation > payment. After validation and calculation in registration page need to go to confirmation page.
My code is
<?php
ob_start();
session_start();
include("header.php");
include("ini.php");
date_default_timezone_set('Asia/Brunei');
header('Cache-Control: max-age=900');
if (isset($_POST['submit'])) {
$error = 0;
//validations
if ($error <= 0) {
//do some calculation
header('location:confirm.php');
}
}
<?php
ob_flush();
?>
Control entered in to if ($error <= 0) clause, but stay on the registration page.
I tried many ways like
header('location:confirm.php', true, 302);
instead of
header('location:confirm.php');
removed
ob_start() and ob_flush()
add exit() after header()
then it goes to blank page.
Also try to use
echo '<script type="text/javascript">';
echo 'window.location.href="confirm.php";';
echo '</script>';
Then the control goes to confirmation page but url seems to be as registration.php
But header('location:confirm.php'); is working fine in my local server. Anybody please help me to resolve this issue.. Is there any alternate way for header(). Looking forward to you guys.. Thanks
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Also try to change header('Location:confirm.php');
(Note L is capital since its work in your local server may be problem with strict php type try this once)
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'confirm.php';
header("Location: http://$host$uri/$extra");
use ob_end_flush() at the end. This should work like charm.
i had the same issues for years but today i discovered that u don't have to have any html or blank space before the header word .Try it out

Checking if a user is logged in with PHP and javascript

I am using a plugin userfrosting to manage users.
The PHP side of the app checks if the user is currently logged in.
The javascript side of the app checks what the permission of the user is.
EG.
<?php
if (isUserLoggedIn()){
?>
<script>
loadCurrentUserPermission();
</script>
<?php
}
?>
Now that's fine and dandy, but now I want to implement some user access restrictions in my PHP files. Can I use such a method inside of a php function? Because I don't want to escape the PHP and check for every action.
The next option would be to store it in a variable, but how can I escape a variable assignment midway to the outcome of a < script > ?
$userPermission;
if (isUserLoggedIn()){
$userPermission = (
?>
<script type="text/javascript">
userLoadPermissions();
</script>
<?php
);
}
?>
Finally what if I use jquery to store the permission level in a PHP session ID? is that safe? Can someone easily modify their own level?
What's wrong with echoing the js?
<?php
if (isUserLoggedIn()){
echo "<script>loadCurrentUserPermission();</script>";
// more php
?>
Edit: Oh, I see. Javascript, being server-side, will be loaded after the PHP, so it's not simple (or even possible) to have PHP, then JS, then more PHP execute on the same page. You could consider calling the rest of the PHP from somewhere else using an AJAX request but that might not be practical for all of your pages.

whether to use php or javascript method for redirecting a page

Whenever I user php header("Location: index.php"), most of time then it does not work. It exactly DOES NOT work without any error nor any run-time fatal error. I don't use any echoing nor I keep empty lines prior to header() call. I double check everthing, but it does not work.
Now, I user JS solution. I write a function that gets the param, store it in an immediately created <input> field and the using Javascript in the same function, I get the value out of input and assign it to window.location.href= value; which then finalizes the redirection. Is my method reliable and good?
My function seems like this:
function redirect($address)
{
?>
<input id='this_address' value="<?php echo $address; ?>" />
window.location.href = document.getElementById("this_address").value;
<?php
}
Are you sure that your index.php is in the same directory as the scripts that calls redirect or vice versa...?
If not try header("location:../../index.php");
Setting and cookies or session variables will cause issues with a redirect. It has to be sent before any output or exchange with the server and the client. Check your browser log for any errors too. It might be helpful in determining why you are not achieving the result you want.
You could also implement both methods, but it would be more useful to find out why your PHP redirect is not effective.
If u can't able to find out the solution for php header prob, Just add the below code at the top of the page. This is not the perfect way, but we can use this...
ob_start();
Why not use Response.Redirect "../../index.php" to redirect to another page.

Pass parameters to location.href in javascript instead if HEADER()

I am new to wordpress.
I have tried the following code
header("Location: $location", true, $status);
I want to redirect the page to particular page and i am getting this kind of error
"Cannot modify header information - headers already sent by (output started at /home/content/c/l/a/clareb23/html/client4/wp-content/plugins/featured-articles-lite/main.php:773) in /home/content/c/l/a/clareb23/html/client4/wp-includes/pluggable.php on line 870"
Any suggestions would be appreciated.
Based on comment
You can do it like this:
<script>
window.location = '<?php echo $location?>?myvar=true&status=<?php echo $status?>';
</script>
Make sure that you do not echo/print anything on screen before header(...) statement and that there is no whitespace before opening php tag eg <?php. It is also good practice to put exit() after header() function.

Categories

Resources