1

I am new to php. I am trying to redirect to some page. At first, I am trying by hardcoding as below, but still not working.

$location = 'https://mywebsite.com/abc/?a=xyz';

header('Location:'.$location);

Above just redirecting till here: https://mywebsite.com/abc/

But I am unable to get query-params at the end of the url. Please someone tell me whats the fix for above?

EDITED:

In logs, I am getting as below warning:

PHP Warning:  Cannot modify header information - headers already sent by (output started at /myfile1.php:34) in /myfile2.php on line 317,

Here is myfile1.php code:

class MyClass32
{
   function myFun() {
?>
<head>


<body>

<div align=center>
<p><br>

<table width=778 cellpadding=0 cellspacing=0 border=0>
   <tr>
      <td bgcolor="#3399cc" width=6>&nbsp;</td>
      <td colspan=3>
         <table width="100%" cellPadding=0 cellSpacing=0 border=0>
        <tr>

<?php // **THIS IS LINE 34**
    //START TOP RIGHT MENU
      $abc = new MyClass($this);
      if ($this->isLog()) $xyz->goInto();
    //END TOP RIGHT MENU 
?>
John
  • 281
  • 1
  • 4
  • 9
  • 1
    That code looks correct (although adding a `die` after the `header` will ensure nothing else runs on the page). Could it be that the page you are redirecting to, `/abc/` itself has some sort of redirect? Post up the content of your `.htaccess` file as well, as that could be working incorrectly. – M1ke Aug 06 '15 at 10:58
  • 1
    use exit(0) instead of die – Konstantin Aug 06 '15 at 10:59
  • possible duplicate of [PHP Header Location with parameter](http://stackoverflow.com/questions/9861925/php-header-location-with-parameter) – Nolwennig Aug 06 '15 at 11:06
  • do you get the query if write such url in address tab of browser? – splash58 Aug 06 '15 at 11:12
  • Presented as-is, the code you have provided should absolutely work. If not, you might want to enable your error reporting (if it's not enabled) to try and figure out what is happening here. Perhaps, for example, [headers are already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php), which generates a PHP Warning. I would also make sure there are no double redirects happening - who knows, perhaps there is code at that location that is redirecting the code again? Open the web inspector in your browser and check for unexpected redirect activity. – Darragh Enright Aug 06 '15 at 11:17
  • @Darragh What you said is correct, I am getting 'headers sent' warning in my logs, but not understand how to fix. I have updated above, could you please look into once. – John Aug 06 '15 at 12:04

2 Answers2

1

There are a few things going on here.

Most importantly, you need to set your header with double quotes " rather than single quotes, please see PHP Header Location with parameter for more information on this.

You also need to set a exit; or die statement after the header to ensure that page processing by PHP ceases once the header is reached.

Your error output is telling you that the page is outputting something to the browser before the header value, this can be anything including a white space, or PHP.

Check things like

    <--- file start
     <?php

    $var...
   header("stuff.php");

There is a white space before the <?php and so this would give a similar error message to yours. Check for these across your code.

View the source of your page myfile1.php, if the source is NOT empty, then you've discovered what's happening, it looks like your class->myfun() is outputting its contents when it is called.

There's probably quite a lot of tidying up and rearrangement of your code you need to do that is beyond the scope of this current question. but importantly, change

function myFun() {
?>
output text
<?php
}

into something more with this syntax:

function myFun(){
$output = "
output text
";
}

This means that you then have control of when and how your data is displayed, so that you can then later in the PHP file approach it more like:

if (true || false ){
    header("Location:some.php");
    exit;
}
else {
    print $output;
}

This is a crude form of Output Buffering : What is output buffering?

Also:

Check things like your .htaccess and other redirects that would cause /index.php?a=xyz to redirect itself to /index.php or otherwise remove the query string, as mentioned by Splash58.

Also:

AVOID giving your variables the name $this, especially in a class method context, What does the variable $this mean in PHP?

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
0

Try this :

$location = 'https://mywebsite.com/abc/';
$params = '?a=xyz'; // set your parameters in variable

header("Location: $location.$params"); // double-quote there

source : PHP Header Location with parameter

Community
  • 1
  • 1
Nolwennig
  • 1,613
  • 24
  • 29
  • 2
    `params = '?a=xyz';` is a Parse Error. You might want to correct that before someone downvotes you. – Darragh Enright Aug 06 '15 at 11:20
  • This does nothing to solve the issue, it simply breaks the issue into several variables. There is no reason `$location` can not also contain the Query String. – Martin Aug 06 '15 at 12:09