Page redirecting???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Page redirecting???

Post by No0b »

I'm trying to make a log in script that once logged in and the script does everything it's supposed to do then it redirects you to another page. How shell I do this? With the header() function?

Thanks for reading and all help is appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I use a combination of header() and meta/Javascript redirection, along with giving them a link in-page for those users that just don't jump to the next page.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

Code please? I have the link that if someone doesn't redirect can click. But I tried:

Code: Select all

header ( "Location: nextpage.php");
But reterns an error:
Warning: Cannot modify header information - headers already sent by (output started at /notgoingtotellyou/html/do_login.php:67) in /notgoingtotellyou/html/do_login.php on line 149
Why does this not work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Post your actual script.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

i usually do this:

Code: Select all

if ($_POST)
{
   // Log the user in
   header('Location: http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\').'/index.php');
}
else
{
   // Post the log-in box
}
The reason I do this is because you have to use a header before any output is written to the browser, I don't write anything to the browser unless nothing has been posted to the page.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

this is the code:

Code: Select all

<?
if ((!$_POST[username]) || (!$_POST[password])) {
	header("Location: show_login.php");
	exit;
}
$db_name = "mydb";
$table_name = "table";
$connection = @mysql_connect("localhost", "id", "pass") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE username = '$_POST[username]' AND password = '$_POST[password]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = mysql_num_rows($result);
if ($num != 0) {

	$cookie_name = "auth";
	$cookie_value = "ok";
	$cookie_expire = "0";
	$cookie_domain = ".domain.com";
	setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);

	//start a session
	session_start();

	//asign the value of the username and password to two superglobals
	$_SESSION[username] = $_POST[username];
	$_SESSION[password] = $_POST[password];
	$_SESSION[logged] = "true";


	if ($_SESSION[logged] == "true") {
		$greetings = "<td width=\"30%\"><div align=\"right\">Hello $_SESSION[username]! <a href=\"logout.php\">Log Out</a> | <a href=\"my_account.php\">My Account</a></div></td></tr>";
	} else {
		$greetings = "<td width=\"30%\"><div align=\"right\">Hello Guest!<a href=\"show_login.php\">Log In</a>|<a href=\"signupform.php\">Register.</a></div></td></tr>";
	}

	$msg = "<br><br><br><br><br><table width=\"300\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" bordercolorlight=\"#999999\" bordercolordark=\"#66CCCC\">
  <tr>
    <td height=\"40\" background=\"/images/Gradient.gif\">
      <div align=\"center\"><b> <font>Logged In</font></b></div>
    </td>
  </tr>
  <tr bgcolor=\"#F3FFFD\">
    <td height=\"62\">
      <p><font size=\"2\">Wellcome $_SESSION[username]!</font></p>
      <p><font size=\"2\">Thank you for logging in. You are being transfered...</font></p>
    </td>
  </tr>
  <tr>
    <td height=\"40\" background=\"/images/Gradient.gif\">
      <div align=\"center\"><font color=\"#000000\"><a href=\"my_account.php\"><font size=\"2\">If
        you're not automaticly redirected click here.</font></a></font></div>
    </td>
  </tr>
</table>
";


} else {
	header("Location: show_login.php");
	exit;
}

sleep(10);
header ( "Location: my_account.php");

?>

<html>
<head>
<title>My Account</title>
<script language="JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>
<body onLoad="MM_preloadImages('images/findanything2.jpg','images/about_us2.jpg','images/get_spot2.jpg','images/classifieds2.jpg','images/login2.jpg')">
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<table width="100%" border="0">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="2" cellpadding="0">
        <tr>
          <td width="63%" height="41"><img src="images/35.jpg" width="244" height="35"></td>
          <td width="37%" height="41">
            <table width="45%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="37%">
                  <input type="text" name="textfield" size="40">
                </td>
                <td width="63%"><img src="images/searchbutton.jpg" width="96" height="35"></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="10%"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','images/findanything2.jpg',1)"><img name="Image6" border="0" src="images/findanything.jpg" width="104" height="18"></a></td>
          <td width="8%"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','images/about_us2.jpg',1)"><img name="Image7" border="0" src="images/about_us.jpg" width="78" height="18"></a></td>
          <td width="7%"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image8','','images/get_spot2.jpg',1)"><img name="Image8" border="0" src="images/get_spot.jpg" width="73" height="18"></a></td>
          <td width="8%"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','images/classifieds2.jpg',1)"><img name="Image9" border="0" src="images/classifieds.jpg" width="82" height="18"></a></td>
          <td width="5%"><a href="show_login.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image12','','images/login2.jpg',1)"><img name="Image12" border="0" src="images/login.jpg" width="52" height="18"></a></td>
          <td width="40%">&nbsp; </td>
<?
echo "$greetings";
?>
        </tr>
      </table>
    </td>
  </tr>
</table>
<br>
<?

echo "$msg";

?>
</body>
</html>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

try making the header() this

Code: Select all

header('Location:/path/to/file/from/root/index.php')
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Bad:

Code: Select all

header('Location: show_login.php');


Good:

Code: Select all

header('Location: http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['PHP_SELF']), '/\\').'/show_login.php);
When using the header function URLs must be absolute.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

It's not the Url. The url works fine just the page isn't seen at all it skips over the html part below the header () func. and if i place the header at the bottom with the html the error pops up.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

thats becasue you are redirecting before it gets to that part.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's what header redirection does.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Did you try the header syntax that I posted?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I don't think he fixed his whitespace issue:

Code: Select all

<?
if ((!$_POST[username]) || (!$_POST[password])) {
    header("Location: show_login.php");
    exit;
}

Remove the space before the opening PHP tag
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

I know that's why it skips over the html cuz it's before the html but when i put the header after the html it errors:
Warning: Cannot modify header information - headers already sent by (output started at /notgoingtotellyou/do_login.php:68) in /notgoingtotellyou/do_login.php on line 95
Post Reply