Page redirecting???
Moderator: General Moderators
Page redirecting???
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.
Thanks for reading and all help is appreciated.
Code please? I have the link that if someone doesn't redirect can click. But I tried:
But reterns an error:
Code: Select all
header ( "Location: nextpage.php");Why does this not work?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
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
i usually do this:
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.
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
}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%"> </td>
<?
echo "$greetings";
?>
</tr>
</table>
</td>
</tr>
</table>
<br>
<?
echo "$msg";
?>
</body>
</html>- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
try making the header() this
Code: Select all
header('Location:/path/to/file/from/root/index.php')- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Bad:
Good:
When using the header function URLs must be absolute.
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);- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
I don't think he fixed his whitespace issue:
Remove the space before the opening PHP tag
Code: Select all
<?
if ((!$_POST[username]) || (!$_POST[password])) {
header("Location: show_login.php");
exit;
}Remove the space before the opening PHP tag