简体   繁体   中英

WordPress redirect issue with php

i have some users in my db. The PHP script pick the users accurately, but redirect not working.

Please help any one my script is below:

if($sqlquery==1)
{
    $_SESSION['email']=$email;
    $_SESSION['password']=$password;
    wp_redirect( 'https://mcqpage.com/somepage', 301 );
    exit; 
} 
else
{
    header("location:http://www.mcqpage.com/index.php"); 
}  

please check it if useful for u

https://www.templatemonster.com/help/wordpress-how-to-create-a-redirect-from-a-home-page-to-any-url-using-the-php-redirect.html#gref

$location = get_site_url() . "/contact";
wp_redirect( $location, 301 );
exit;

my detail are as follow

login page

<form onsubmit='return formValidator()' action='https://mcqpage.com/connection/' method='post'>
        <h2>Enter Your Details</h2><hr/>
        <label>Name :</label>
        <input type='text' name ='addr1' id='addr' /><br />

        <label>Password :</label>
        <input type="password" name="password" id="password">



        <input type='submit' value='Check Form'  />

connection page

> [insert_php]

session_start();

if($_POST['addr1']!="" && $_POST['password']!=""){

session_start();

$connec=@mysql_connect("") or
 mysql_error();

        if (!$connec) {

    die('Could not connect: ' . mysql_error());

}

    $email=$_POST['addr1'];

//echo"$email";

$password= $_POST['password']);  

if ($email=="")
 {
    echo "Enter User Name.......";
 }

else


    $result = mysql_query("SELECT * FROM `database`.`table` WHERE 

password='$password' and name='$email'");

    $data = mysql_num_rows($result);



    if($data==1)
      {
         $_SESSION['email']=$email;

          $_SESSION['password']=$password;

wp_redirect( 'https://mcqpage.com/page', 301 );

 exit; 

      } 
    else
    {
        header("location:http://www.mcqpage.com/index.php"); 
    }  
 }

//connection closed
mysql_close ($connec);}
else{
        header("location:http://www.mcqpage.com/index.php"); 
}
[/insert_php]

Are you performing your redirect after the headers have already been sent? If so it will not work. Please make sure that your code is running before the headers are sent, preferably as part of the template_redirect hook that Wordpress offers.

If you are not sure where your code is being loaded I suggest you incpsulate your code inside a function that runs during the template_redirect hook. For example, you can add the following to your theme's functions.php file:

function handle_session_data()
{
  //Your code here.
}
add_action( 'template_redirect', 'handle_session_data' );

@Nadav My error_log shows the following error:

[02-May-2017 07:29:06 UTC] PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/mysite/public_html/wp/wp-includes/class.wp-styles.php:237) in /home/mysite/public_html/wp/wp-content/plugins/insert-php/insert_php.php(47) : eval()'d code on line 2

[02-May-2017 07:29:06 UTC] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/mysite/public_html/wp/wp-includes/class.wp-styles.php:237) in /home/mysitec/public_html/wp/wp-content/plugins/insert-php/insert_php.php(47) : eval()'d code on line 2

[02-May-2017 07:29:06 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/mysitec/public_html/wp/wp-includes/class.wp-styles.php:237) in /home/mysite/public_html/wp/wp-content/plugins/insert-php/insert_php.php(47) : eval()'d code on line 25

the following code solved my problem

`function register_my_session()

{

if( !session_id() )

{

session_start();

}

}` in function.php

and start the session without any error, for header, i used the java script redirect, this solved my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM