DeanWhitehouse Posted April 21, 2008 Share Posted April 21, 2008 Is it possible to have a login(input) form and the login(proccessor) in the same file?? And if so, can i been shown an example and how to post the form to self Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/ Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 <?php if (isset($_POST['submitted'])) { //...process form here } ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <more><form><stuff> <input type="hidden" name="submitted" value="true" /> </form> Like that. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522433 Share on other sites More sharing options...
redarrow Posted April 21, 2008 Share Posted April 21, 2008 Try this m8.......... <?php if(isset($_POST['submit'])){ $message=$_POST['message']; echo $message; } ?> <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>"> <br><br> Your meassage please! <br> <input type="text" name="message"> <br><br> <input type="submit" name="submit" value="send"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522435 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Same thing as what I said, basically. Should work perfectly fine. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522438 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 ok, thanks i changed my code like this <?php require_once '../includes/main.inc.php'; require_once '../includes/db_connect.php'; require_once '../includes/config_table.inc.php'; session_start(); // Only include the header and footers if you have to print errors function print_error($err_message) { require_once '../includes/header.php'; require_once '../includes/footer.php'; require_once '../nav_bar.php'; echo $err_message; exit; } if(isset($_POST['admin_login'])){ $user_name = $_POST["user_name"]; $user_password = $_POST["user_password"]; $verify_username = strlen($user_name); $verify_pass = strlen($user_password); if ($verify_pass > 0 && $verify_username > 0) { $salt = substr($user_password, 0, 2); $userPswd = crypt($user_password, $salt); $sql = "SELECT * FROM `$user` WHERE user_name='$user_name' AND user_password='$userPswd' LIMIT 1;"; $result = mysql_query($sql); if (mysql_num_rows($result) == 1){ $row = mysql_fetch_assoc($result); $user_level = $row['userlevel']; if ($user_level == 1) { $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); header("Location:http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]); $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } elseif ($user_level == 2){ $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); echo "<meta http-equiv='refresh' content='0; url=../index.php'>"; $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } } else{ print_error( 'Login failed. Username and Password did not match database entries.'); } } else { print_error( "Form was not completed. Please go back and make sure that the form was fully completed."); } mysql_close(); ?> <html> <table bgcolor='#999999' align='right'><form action="<?php $_SERVER['PHP_SELF']; ?>" method='POST'> <tr><td>Username: </td><td><input type='text' name='user_name' /><br /></td></tr> <tr><td>Password:</td><td> <input type='password' name='user_password' /><br /></td></tr> <tr><td><input type='submit' value='Continue' name='admin_login' /></td></tr> </form></table> </html> but i get an error saying Parse error: syntax error, unexpected $end in /home/www/deanwhitehouse.awardspace.co.uk/admin/admin_login.php on line 78 this is the bottom line. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522451 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 The $end issue means you missed a curly brace on a loop or something. And don't do this: <input type="submit" name="admin_login"> Do this: <input type="hidden" name="admin_login" value="true"><input type="submit" value="Submit!"> Otherwise, it won't work properly. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522459 Share on other sites More sharing options...
redarrow Posted April 21, 2008 Share Posted April 21, 2008 missing brace.... <?php require_once '../includes/main.inc.php'; require_once '../includes/db_connect.php'; require_once '../includes/config_table.inc.php'; session_start(); // Only include the header and footers if you have to print errors function print_error($err_message) { require_once '../includes/header.php'; require_once '../includes/footer.php'; require_once '../nav_bar.php'; echo $err_message; exit; } if(isset($_POST['admin_login'])){ $user_name = $_POST["user_name"]; $user_password = $_POST["user_password"]; $verify_username = strlen($user_name); $verify_pass = strlen($user_password); if ($verify_pass > 0 && $verify_username > 0) { $salt = substr($user_password, 0, 2); $userPswd = crypt($user_password, $salt); $sql = "SELECT * FROM `$user` WHERE user_name='$user_name' AND user_password='$userPswd' LIMIT 1;"; $result = mysql_query($sql); if (mysql_num_rows($result) == 1){ $row = mysql_fetch_assoc($result); $user_level = $row['userlevel']; if ($user_level == 1) { $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); header("Location:http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]); $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } elseif ($user_level == 2){ $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); echo "<meta http-equiv='refresh' content='0; url=../index.php'>"; $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } } else{ print_error( 'Login failed. Username and Password did not match database entries.'); } } else { print_error( "Form was not completed. Please go back and make sure that the form was fully completed."); } } //<<<<<<<<< was missing. mysql_close(); ?> <html> <table bgcolor='#999999' align='right'><form action="<?php $_SERVER['PHP_SELF']; ?>" method='POST'> <tr><td>Username: </td><td><input type='text' name='user_name' /><br /></td></tr> <tr><td>Password:</td><td> <input type='password' name='user_password' /><br /></td></tr> <tr><td><input type='submit' value='Continue' name='admin_login' /></td></tr> </form></table> </html> Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522461 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 Ah,how did you find it so fast Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522462 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Ah,how did you find it so fast I believe the code tags on this forum highlight PHP syntax and he saw which one didn't match up. I was far too lazy to look. Sorry. I'm kind of in the middle of coding something myself, so I didn't actually look too hard for the brace that was missing. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522467 Share on other sites More sharing options...
monkeypaw201 Posted April 21, 2008 Share Posted April 21, 2008 just a general question on this topic, i have heard that the scripts that the form posts to must be ABOVE the html, is this true? Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522468 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 no problem. i now have another problem, (acutally the reason i tried this way was to overcome the problem). i need the form to redirect after the user logged in depending on there userclass. It is meant to redirect to the page they where on, but the header information is already been sent by another file, can anyone see a way to get around this? header.php <?php session_start(); echo ("<title>$site_title</title>"); echo ("<link rel='stylesheet' type='text/css' href='../Themes/style.css' />"); echo ("<table class='title'><tr><td align='center'><h1>$custom_header</h1></td></tr></table>"); ?> this is where the header information is already sent from this is where i need to redirect if ($user_level == 1) { $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); header("Location:http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]); $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } elseif ($user_level == 2){ $login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'")); $userright = array($login_check['user_name'], $login_check['userlevel']); $s_userpass = serialize($userpass); setcookie( "$cookiename" , $s_userpass , time()+$custom_time , "" , "127.0.0.1" ); echo "<meta http-equiv='refresh' content='0; url=../index.php'>"; $_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files $_SESSION['username'] = $row['user_name']; $_SESSION['user_level'] = $row['userlevel']; } } Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522471 Share on other sites More sharing options...
GregL83 Posted April 21, 2008 Share Posted April 21, 2008 you could echo javascript.... also I use <?php echo $_SERVER['PHP_SELF']; ?>, don't know if that matters echo the javascript widow.location = "index.html" or your location requested.... Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522474 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Sure thing. At the top of your page, right below (or above) session_start();, put ob_start();. This starts output buffering. It stores ALL output so you can send headers whenever you want (before calling the ending function). To clean the buffer and destroy it WITHOUT displaying its contents, use ob_end_clean();. To display its contents and destroy the buffer, use ob_end_flush();. Put ob_end_flush(); at the end of any script with ob_start() on to make sure you empty the buffer (although PHP does it automatically, it's good practice to do it anyway). Any point with exit() or anything, put ob_end_flush(); before it to display the contents before killing the script. You'll figure it out after playing with it a bit. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522475 Share on other sites More sharing options...
Fadion Posted April 21, 2008 Share Posted April 21, 2008 or alternatively u can use meta refresh if ure lazy on this. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522477 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 or alternatively u can use meta refresh if ure lazy on this. Output buffering is cool though. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522478 Share on other sites More sharing options...
redarrow Posted April 21, 2008 Share Posted April 21, 2008 @ dark_water that a get out clouse to the problam lol.......... in realty you need to correct the problam not rely on ob_start() and ob_flush............. if it broke fix it otherwise leave it alone......... meta refresh better i say........... Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522479 Share on other sites More sharing options...
monkeypaw201 Posted April 21, 2008 Share Posted April 21, 2008 just a general question on this topic, i have heard that the scripts that the form posts to must be ABOVE the html, is this true? Not to change the topic... but anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522483 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 as shown in the code i tried meta refresh and header and both present with the same issue, also i can't set the cookies either. the header file needs to be changed or the something else. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522488 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Not true, but it IS true if you don't want the HTML displayed every time. @redarrow: It's not just a cheap fix for the problem. It stores the output so you can write the code in a fluid manner. I use ob_start() whenever I need to use headers AND write to the browser. Meta redirects are okay for redirects, but what about a cookie? ob_start() helps there too. It's a great function. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522490 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 so do people think i should use ob_start(); or try and change the code and find a way to make it work Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522496 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Use ob_start();. Look at the manual pages on it if you must, and figure it out. It'll help you SO much in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522499 Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2008 Author Share Posted April 21, 2008 y do i need to use the flush and clean?? Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522501 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Use flush to display what's in the buffer at certain points if you want, and use clean before a redirect so PHP doesn't need to do it itself. Saves time and resources, basically. Quote Link to comment https://forums.phpfreaks.com/topic/102066-login-form-form-actionself-how-to-submit-data-to-self/#findComment-522502 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.