Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Redirect from a page with PHP and Smarty

smarty php redirect

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 26 January 2013 - 10:00 AM

Hello,
I have a small problem. I want my signup page to be redirected to my home page after submitting the signup form. I tried this (my .tpl file):
<html>
<body>
<form name = 'signupform' id = 'signupform' method = "post" action = "signup.php" >
  <table border="0" cellpadding="5" cellspacing="0">
   <tr>
    <td>username:</td>
    <td><input type="text" name='user_name' /></td>
   </tr>
   <tr>
    <td>name:</td>
    <td><input type="text" name='first_name' /></td>
   </tr>
   <tr>
    <td>Password:</td>
    <td><input type="password" name='password'/></td>
   </tr>
   <tr>
    <td><br/></td>
    <td><input  id = 'signupbutton' type='submit'  value='signup' onclick = ""/></td>
   </tr>
  </table>
</form>
</body>
</html>
header( 'Location: myindex.php' ) ;

Could you tell me how I can do this?
Thank you so much.

#2 grisha

grisha

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts

Posted 26 January 2013 - 11:41 AM

You can set the response headers only before sending any content to the browser. You've placed header() call at the bottom of the page - that's why it has no effect.

You should place that header() call somewhere in the "signup.php" file in a conditional block determining that the user has signed up - before the rendering or any other response sending takes place.

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 26 January 2013 - 07:15 PM

You can set the response headers only before sending any content to the browser. You've placed header() call at the bottom of the page - that's why it has no effect.

You should place that header() call somewhere in the "signup.php" file in a conditional block determining that the user has signed up - before the rendering or any other response sending takes place.


Totally accurate, don't take it as a fix to your program but a concept since you will always need to do it that way since headers are inherent to protocol and as such they have an order and a reason to be first.

#4 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 26 January 2013 - 07:36 PM

I understand it now. Thank you for both answers!

#5 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 27 January 2013 - 09:35 AM

Hi I have another question related to it, I did what you said. I put header call to signup.php file but it didn't work. When I submit the form, I see the signup.php, not myindex.php. I'd appreciate any help, thanks in advance. Here is my signup.php :

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "ozoverflow";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("veritabanına erisilemedi");
mysql_select_db($mysql_database, $bd) or die("veritabanı secilemedi");


$user_name = $_POST['user_name'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$password = $_POST['password'];
$email = $_POST['email'];


$signup_query="INSERT INTO user (user_name, first_name, last_name, email, password) VALUES ('$user_name', '$first_name', '$last_name', '$email', '$password')";

$signup_result=mysql_query($signup_query);


if($signup_result)
{ header('Location : myindex.php');}
else
{ echo "an error occured" . mysql_error(); }




?>


#6 grisha

grisha

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts

Posted 27 January 2013 - 09:43 AM

You're having a blank line at the start of the file? Is it UTF8 without BOM?

#7 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 27 January 2013 - 09:53 AM

no and no :(

#8 grisha

grisha

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts

Posted 27 January 2013 - 09:55 AM

Set error_reporting to E_ALL, and display_errors to true and let me now if it displays anything to you. More than that - check if you don't have header() funciton disabled - make a simple php file with the following:

<?php header('Location: http://google.com');

And visit this simple file in the browser - let me now what are the results.

#9 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 27 January 2013 - 10:28 AM

error_reporting and display_errors are already as you mentioned. And I tried your code and it directed to google.com.
And I wrote

header('Location: http://google.com');

to signup.php, it also worked there.
Thank you very much for your helping. I tried different combinations. the simple file directed to my.index.php, but not the signup.php
I'm sending the final versions of my codes :

register.php

<?php
// load Smarty library
require('C:\wamp\smarty\libs\Smarty.class.php');
$smarty = new Smarty();
$smarty->display('kayit.tpl');

?>



register.tpl


<html>
<body>
<form name = 'signupform' id = 'signupform' method = "post" action = "signup.php" >
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td>username:</td>
<td><input type="text" name='user_name' /></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" name='first_name' /></td>
</tr>
<tr>
<td>lastname:</td>
<td><input type="text" name='last_name' /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name='email' /></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name='password'/></td>
</tr>
<tr>
<td><br/></td>
<td><input  id = 'signupbutton' type='submit'  value='register' /></td>
</tr>
</table>
</form>
</body>

</html>


signup.php



<?php

$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "ozoverflow";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("veritabanına erisilemedi");
mysql_select_db($mysql_database, $bd) or die("veritabanı secilemedi");


$user_name = $_POST['user_name'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$password = $_POST['password'];
$email = $_POST['email'];


$signup_query="INSERT INTO user (user_name, first_name, last_name, email, password) VALUES ('$user_name', '$first_name', '$last_name', '$email', '$password')";

$signup_result=mysql_query($signup_query);


if($signup_result)
//header('Location: http://google.com');
header('Location : myindex.php');
else
{ echo "error occured" . mysql_error(); }
?>


Again thank you very much!

#10 grisha

grisha

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts

Posted 27 January 2013 - 10:36 AM

I'm a bit confused - does it work right now as you wanted? :)

#11 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 27 January 2013 - 10:37 AM

no it does not :/
it works with all combinations except the one I wanted.

#12 grisha

grisha

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 106 posts

Posted 27 January 2013 - 10:51 AM

Remove the space before the ":"

header('Location: myindex.php');





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download