Derleek Posted May 15, 2008 Share Posted May 15, 2008 what is the syntax to redirect a user to a specific page? i have a script set up for user verification and i want it to redirect to a specific page when the user name and password are correct... can't seem to find the syntax for it in the php manual Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/ Share on other sites More sharing options...
947740 Posted May 15, 2008 Share Posted May 15, 2008 You could use javascript and PHP. <?php echo <<<END <script language='javascript' type='text/javascript'> location.href = 'file.php'; </script> END; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542196 Share on other sites More sharing options...
soycharliente Posted May 15, 2008 Share Posted May 15, 2008 <?php header("Location: page.php"); exit; ?> Some users disable JS, so using all PHP might be best. Make sure that you have not output any data before using this or it won't work. Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542197 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Share Posted May 15, 2008 header(); or meta refresh if you get a header error, try adding ob_start(); to the beggining of the script Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542199 Share on other sites More sharing options...
947740 Posted May 15, 2008 Share Posted May 15, 2008 OMG. Total lapse of concentration there. I was in JavaScript mode there... Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542200 Share on other sites More sharing options...
Derleek Posted May 15, 2008 Author Share Posted May 15, 2008 I'm validating the info on the login.php page so i don't know if header() will work... does this require that i don't output any HTML data, or just don't use any PHP outputs? how does meta refresh work? Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542203 Share on other sites More sharing options...
947740 Posted May 15, 2008 Share Posted May 15, 2008 You cannot have any output whatsoever before a header(); Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542204 Share on other sites More sharing options...
Derleek Posted May 15, 2008 Author Share Posted May 15, 2008 ok, then how would i go about sending a user to, lets say, randomPage.php after i have validated their info on login.php... Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542207 Share on other sites More sharing options...
947740 Posted May 15, 2008 Share Posted May 15, 2008 As long as you do not have output, just: <?php // Currently in login.php // Make sure they are a user with valid info // Redirect the user header("Location: randomPage.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542211 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Share Posted May 15, 2008 when you have check that the user is the user, put header("Location:randompage.php"); remeber put ob_start(); at the beggining of the page, after session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542212 Share on other sites More sharing options...
soycharliente Posted May 15, 2008 Share Posted May 15, 2008 With good coding and proper structure, there's really no need to ever use ob_start() IMO. If you think about it, you shouldn't ever have any output before the possibility of redirecting the page. Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542217 Share on other sites More sharing options...
Derleek Posted May 15, 2008 Author Share Posted May 15, 2008 sorry, let me be more clear. here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Login</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> screen name: <input type="text" name="login" maxlength="25"><br> password: <input type="password" name="pword" maxlength="25"><br> <input type="submit" name="submitbtn" value = "Login!"> </form> or Signup <a href="SignUp.php">here</a> <br> <?php //validation code would be here... if(username && password are correct) { //would be code to send user to game.php } } ?> </body> </html> i'd prefer to do the validation with a script on login.php, as i understand it is more efficient. So maybe i am in need of a way to do this with out generating any output... maybe the term i am looking for is not 'redirect'. I simply need to send the user to a new page after the password and user name are varified Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542223 Share on other sites More sharing options...
947740 Posted May 15, 2008 Share Posted May 15, 2008 I would just create another script that handles the login. Other than that, you would have to rebuild your script so you can send headers. To me, it just makes more sense to have a separate script. Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542227 Share on other sites More sharing options...
Derleek Posted May 15, 2008 Author Share Posted May 15, 2008 it just seems like it would be more efficient to validate the form before even $_POSTing it... that way if a user doesn't have the correct information there isn't even any need for a redirect? Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542233 Share on other sites More sharing options...
Derleek Posted May 15, 2008 Author Share Posted May 15, 2008 well how would i rebuild it to send headers? Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542237 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Share Posted May 15, 2008 you would need to use, if(isset($_POST['])) { } statements and then have the valdiation code in there Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542241 Share on other sites More sharing options...
soycharliente Posted May 15, 2008 Share Posted May 15, 2008 Just move the validation to the top of the page. <?php //validation code would be here... if(username && password are correct) { //would be code to send user to game.php } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Login</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> screen name: <input type="text" name="login" maxlength="25"><br> password: <input type="password" name="pword" maxlength="25"><br> <input type="submit" name="submitbtn" value = "Login!"> </form> or Signup <a href="SignUp.php">here</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542268 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Share Posted May 15, 2008 that be checking everytime you go to the page, mine would just check when they click submit Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542270 Share on other sites More sharing options...
soycharliente Posted May 15, 2008 Share Posted May 15, 2008 @blade: I assumed the validation included some kind of check to see if the form was submitted. @op: Here's some basic code I always start with when I build a login page. Maybe you can get some use out of it too. <?php session_start(); function myEscape($string) { dbconnect(); $new = get_magic_quotes_gpc() ? stripslashes($string) : $string; $safe = mysql_real_escape_string($new); dbclose(); return $safe; } if ($_POST["Submit"] == "Login") { foreach ($_POST as $key => $val) { $_POST[$key] = myEscape($val); } $un = $_POST['Username']; $pw = md5($_POST['Password']); dbconnect(); $sql = "SELECT * FROM table WHERE username='{$un}' AND password='{$pw}'"; $result = mysql_query($sql) OR DIE ("Unable to validate login."); dbclose(); if (mysql_num_rows($result) > 0) { $r = mysql_fetch_assoc($result); $user = $r["username"]; $pass = $r["password"]; if ($un == $user && $pw == $pass) { $_SESSION['Login'] = TRUE; header("Location: data.php"); exit; } else { $_SESSION['Login'] = FALSE; $error = TRUE; } } else { $_SESSION['Login'] = FALSE; $error = TRUE; } } ?> <html> <body> <form action="login.php" method="post"> <p> <label for="Username" id="Username">Username:</label><br /> <input type="text" name="Username" value="" maxlength="20" /> </p> <p> <label for="Password" id="Password">Password:</label><br /> <input type="password" name="Password" value="" maxlength="20" /> </p> <p> <label for="Buttons" id="Buttons">Done?</label><br /> <input type="submit" name="Submit" value="Login" /> - <input type="reset" name="Reset" value="Reset" /> </p> </form> <?php echo ($error) ? "<p class=\"error\">Login error.</p>" : ""; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542271 Share on other sites More sharing options...
Derleek Posted May 16, 2008 Author Share Posted May 16, 2008 ok, i tried moving my script above the <html> tags and removing all of the output... but i still get the output error which is Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\motogame\Login.php:2) in C:\xampp\htdocs\motogame\Login.php on line 54 here is my entire login.php file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <?php function sanityCheck($string, $type, $length){ // assign the type $type = 'is_'.$type; if(!$type($string)) { return FALSE; } // to check if there is anything in the string elseif(empty($string)) { return FALSE; } // to check how long the string is elseif(strlen($string) > $length) { return FALSE; } else { // if all is well, we return TRUE return TRUE; } } function checkSet(){ return isset($_POST['login'], $_POST['pword']); } if(checkSet() !=FALSE) { if(empty($_POST['login'])==FALSE && sanityCheck($_POST['login'], 'string', 25)!=FALSE && empty($_POST['pword'])==FALSE && sanityCheck($_POST['pword'], 'string', 25)!=FALSE) { $link = @mysql_connect('localhost', 'root', 'zOinks12'); if (!$link) { die('Not connected : ' . mysql_error()); } $db_selected = mysql_select_db('moto', $link); if (!$db_selected) { die ("Database not selected : " . mysql_error()); } $query = "SELECT realName, screenName, password, uID FROM fans"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if($row[screenName]==$_POST['login']) { if($row[password]==$_POST['pword']) { header ("Location: moto_game.php"); exit(); } else { exit(); } } } } else { exit(); } } ?> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Login</title> </head> <body> <form action="Login.php" method="post"> screen name: <input type="text" name="login" maxlength="25"><br> password: <input type="password" name="pword" maxlength="25"><br> <input type="submit" name="submitbtn" value = "Login!"> </form> or Signup <a href="SignUp.php">here</a> <br> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542583 Share on other sites More sharing options...
soycharliente Posted May 16, 2008 Share Posted May 16, 2008 Make sure that you have not output any data before using this or it won't work. You can't output any HTML before the header() call. Move he DOCTYPE after all the PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/105796-solved-redirection/#findComment-542766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.