John
John

Reputation: 291

php redirect to page with params

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 
?>

Upvotes: 1

Views: 288

Answers (2)

Nolwennig
Nolwennig

Reputation: 1684

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

Upvotes: 0

Martin
Martin

Reputation: 22760

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?

Upvotes: 1

Related Questions