Register Page:
$_SESSION['namex']=$_POST['username'];
$_SESSION['passx']=$_POST['pass'];
$_SESSION['confirmpassx']=$_POST['confirmpass'];
$_SESSION['emailx']=$_POST['email'];
$_SESSION['confirmemailx']=$_POST['email2'];
$_SESSION['keyx']=$_POST['byond'];
Index Page
$namex=$_POST['username'];
$passx=$_POST['pass'];
$pass2x=$_POST['confirmpass'];
$emailx=$_POST['email'];
$email2x=$_POST['email2'];
$ByondKeyx=$_POST['byond'];
str_replace($namex,$_SESSION['namex'],$namex);
str_replace($passx,$_SESSION['passx'],$passx);
str_replace($pass2x,$_SESSION['confirmpassx'],$pass2x);
str_replace($emailx,$_SESSION['emailx'],$emailx);
str_replace($email2x,$_SESSION['confirmemailx'],$email2x);
str_replace($keyx,$_SESSION['keyx'],$keyx);
Alright, so what these codes are supposed to do is the Register Page is doing it's thing (I think) and the Index page is a page that if something is wrong like used Username or password or the Username is invaild then the index page comes in and makes another form with the same data as the Register page. sorry if it's hard to read/understand. If you wanted to see my website and see what I mean then here's the link: http://snyp.freeoda.com
Thanks for reading and please help.
Add session_start() to the top of each file
And add $_SESSION['varname'] in front of each str_replace call. (or whatever you're trying to assign to, can't quite tell)
Do you have the session_start(); function call at the very beginning of your code?
Note: No newlines, whitespaces are allowed before it.
First off, this answer assumes you have session_start() at the top of both scripts and that no errors / warnings are visible when the page is shown (using error_reporting(-1) and ini_set('display_errors', 'On').
Your register page stores the posted fields inside the session:
$_SESSION['namex']=$_POST['username'];
That's all fine; let's assume that you redirect back to the index page. The index page can then take the values from the session directly:
$namex = $_SESSION['namex'];
Assuming the form on the index page uses $namex to build the form.
Related
I am working with three PHP files. Two serve as web pages and the other in an external server side script. The server side script is included in both web pages files and what I want to do is have some buttons on the first page and depending on which one is clicked, populate the second page with data and then redirect to it.
With the code below, the idea was to pick up the button click, figure out which button was clicked, and then call the function to run the proper query and set the needed variables. I don't understand why the variable is not getting set.
Thanks to anyone looking at this!
First page's button (index.php):
<input type="submit" id='details' name='details' value='Submit'/>
Second page where variable is undefined when page is loaded:
<h4><?php echo $selected_button; ?></h4>
External script:
function detailBuilder(){
$selected_button = "Option One";
//header('Location: details.php'); if this is here, the page still redirects but the variable doe not get set
//More will happen here once it works
}
if(isset($_POST['details'])){
detailBuilder();
header('Location: details.php');
}
As several people have pointed out previously, you redirect to another file. At that point, all locally defined variables are gone - you no longer have access to them.
Look into PHP sessions in PHP's documentation. Sessions will allow you to transfer these variables from request to request. However, sessions will only work if you are running some form of webserver.
UPDATE: Also to note, as other people (once again) have pointed out, $selected_button = "Option One" will ONLY apply inside the "scope" of the function detailBuilder. So calling detailBuilder() creates a variable called $selected_button inside the function, and then immediately discards it.
UPDATE 2.0: Sorry for so many updates. Here's an example of setting a session:
Update 3.0: updated code slightly
First things first. Make sure you start the session.
session_start();
You're going to have to call session_start() at the start of any php script! That means that the first file that executes every time should have session_start() at the top.
External Script:
$_SESSION["selected_option"] = "Option One";
Script where originally it was undefined:
$selected_details = $_SESSION["selected_option"];
?>
<h4><?=$selected_details?></h4>
I'm creating a form. There is some server-side validation being executed in a php file separate from the html file containing the form. If the validation fails, I want to redirect back to the original html page with a error message saying what went wrong.
Right now, I am able to successful validate and redirect back to the html page with header(), but I'm not sure how to create and place the error message. Is it possible to check at the top of the html page with php if it's been redirected to through header()? If so, that would solve the problem...
Thanks!
there are several methods to do this i think.
1 add get parameters like:
<input type="hidden" name="formsent" value="1" />
then add method get to your <form>
when you redirect from the other page,, the get would be in the link so you could send it back
header("Location: http://localhost/yourform.php/?{$_GET['formsent']}");
or you could do the validation in the post
if (isset($_POST) && !empty($_POST)) {
do stuff here.. if all is ok go to next page otherwise show errors
}
or you could add a var into a session using $_SESSION['formsent'] = 1 after the post then u could check that also.
its up 2 u
You should set a variable using PHP sessions.
Form page
session_start();
$_SESSION["formerror"] = "Error code here";
header("Location: http://www.example.com");
Redirected to page
session_start();
$errorcode = $_SESSION["formerror"];
// Now convert the error code to something readable and print it out if needed.
IMO this is much cleaner than a GET variable.
As #Mark wrote, you can send a message in a variable by the url in your header() (I mean url + "?variable=$variable") and capture the message in your page (now php page) by $_GET. The message will depend on your validation
Of course you can check: https://stackoverflow.com/a/872522/2737474 (#Jrgns)
Different ways to pass one variable between pages.
In my opinion, you must be careful in choose one of those:
-If you would use one value for many pages (keeping in mind it would be store on server), it would be better to use SESSION.
-If you would use one value for only two pages, it would be better to use GET or POST (depending on your situation, url/form).
-If you would use one value for many pages and want to keep it between sessions (keeping in mind it would be store on client), it would be better to use COOKIE.
You can do this with using $_GET[] method
If validation is successful then redirect to url like
form.php?status=1 // 1 for success
If validation is failed then redirect to
form.php?status=0 // 0 for fail
In form.php which is your form page.
use simple if-else condition
if(isset($_GET['status']))
{
if($_GET['status']==0)
echo'something went wrong';
//else nothing
}
As many clever users wrote you have several methods how to achive this (I won't write all of these):
1st Use sessions check Daniel's answer
2nd Use GET check Sanket Shembekar's answer
3rd Use rZaaaa's answer, but you can enchant it :D
Enchant:
Page 1
header('error: true');
Page 2
print_r(headers_list()); //and find your error
I’m pretty much a complete beginner when it comes to PHP and have been having some problems with displaying a Welcome Username message on a HTML page and where to place session_start(); on the page.
Here’s the relevant part of the HTML page: can one place PHP directly into the page like this?
<div class="col-sm-4">
<h2><p>Welcome, <?php echo $_SESSION['Username']; ?>.</p></h2>
Right at the top of the page (before the HTML) I’ve also put:
<?php
session_start();
?>
<!doctype html>
(is this session start in the right place?)
Not sure if I’m going about this in the right way though :/
Thank you very much for your time; very much appreciate it as well as other helpful replies here.
EDIT Everything works once I changed the pages extension to PHP rather than HTML
can one place PHP directly into the page like this?
Yes. Gathered you use the <?php ?> tags, like you did, and the file has the .php extension; you can.
(is this session start in the right place?)
Also correct.
You haven't said what error you're receiving but I'm assuming you'll have a undefined index 'Username' error (if you don't, turn ON php error reporting and you'll probably see it) this happens because, unless you set the value previously, your 'Username' index inside the $_SESSION array was not defined by $_SESSION['Username'] = "Mosh Mage"
Hope this helps :)
Your code is correct but never forget to store the user name to $_SESSION['username'] or else your it will echo nothing. Before you can store user information in your PHP session, you must first start up the session.
Note: The session_start() function must appear BEFORE the html tag: in your case correct as well.
School yourself with sessions at http://www.w3schools.com/php/php_sessions.asp
I was following a tutorial I found on how to create a simple login using sessions and a database. I followed it to the T (with the exception of tidying up all of the code because theirs was a mess and I'm OCD like that).
I get no errors at all on the page, it just comes up with a blank screen and I can't work out for the life of me why it's doing. I've been trying to get it working for the best part of about 3 hours.
There are 4 files:
index.php - Contains the form for the login script
login.php - Where the form data is processed, which is "require_once"'d into the index.php page at the very start.
config.php - Database connection info
cpanel.php - Where I want the user to be sent once they logged in
And here are those 4 files in action (although I guess they're not in action since they don't actually work!):
index.php
login.php
config.php
cpanel.php
And here's the tutorial I used.
Lastly here's a link to the original (non-source) index.php file
Hope you guys can help, it's driving me crazy now.
Just change
if($jackin) {
to
if(isset($jackin)) {
in login.php file
Also put ini_set('short_open_tag',1)
in your cpanel.php file if short_open_tag is disabled in php.ini
You should try error_reporting(E_ALL); for additional Error output. Check all POST Variables with an echo() / var_dump(), Check the Ifs also with echo() and make sure thats everything is OK.
The echo for $error is doubled.
Additional you should not use the Location Element on the Header with an relative Path.
I have a login form in every page of a website so the user can login from everywhere. I have a login.php file that I refer to it from the form (using 'action').
I use $_SERVER['HTTP_REFERER'] to redirect the user to the same page he logged in from when he succesfully log in or when he logs out.
But if there was a problem logging in, how can I send an error to the same page he is trying to log in?? I have tried sending the error using $_GET, like this:
// process the script only if the form has been submitted
if (array_key_exists('login', $_POST)) {
// Login code goes here...
// If there was a problem, destroy the session and prepare error message
else {
$_SESSION = array();
session_destroy();
header('Location: '.$_SERVER['HTTP_REFERER'].'?error');
exit;
}
But the problem is that a lot of pages in the website are like this details.php?mid=0172495. They already recive information from the $_GET method and for security reasons I cant use another $_GET method...
So, How can I pass the error???
Thanks...
Since you're already using sessions, after you destroy the session why not create a new one with $_SESSION['error'] or something similar set? Or alternatively simply don't delete the session at all but set the error which you can immediately check in other pages?
To add to what Chad Birch said...
In your login script where you redirect, check the HTTP_REFERER value for the character '?'. If it is present, append '&error' to the HTTP_REFERER and redirect to that. Otherwise append '?error' to the HTTP_REFERER and redirect to that.
I'm not sure what exactly you mean by "for security reasons I cant use another $_GET method", but in the case that there's already something in the query string, you just need to append another variable to it, instead of replacing it.
That is, if the address is like details.php?mid=0172495, you should be sending them to details.php?mid=0172495&error, whereas if it was just details.php, you send them to details.php?error.
Another way of doing what you need is to include your login.php file in every page that has the login form and just post to that same page. So you won't need any redirection at all.
This maybe is not a good scalable and maintainable solution, but it is simple. It all depends what kind of app you are writing. But you are saying that you are new to php so you can start like this. You can always go fancy later...