So I am extremely new to PHP and am probably making a stupid mistake. I have searched for quite a while, though, and have not been able to figure out what is going wrong.
Using XAMPP all of the PHP works fine. When I load it onto my web server, however, the if statement regulating the log in form displays both the 'log in' form and the 'log out' form at the same time. Obviously not what I want!
Here is the code snippet:
<?php
if ( isset( $_SESSION['email'] ) ) { // Check to see if user is logged in. If so, display log out button.
?>
<form class="navbar-form pull-right" action="logout.php" method="POST">
<button name="submit" type="submit" class="btn btn-success">Log Out</button>
</form>
<?php
} else { // display log in form
?>
<form class="navbar-form pull-right" action="login.php" method="POST">
<input name="email" type="text" class="span2" placeholder="email">
<input name="password" type="password" class="span2" placeholder="password">
<button name="submit" type="submit" class="btn btn-success">Log In</button>
</form>
<?php
}
?>
This is contained in a file index.php
The basic logic is test to see if an email variable has been created for the session (done in a login.php execution) to check if the user is logged in.
I have no idea if this is the best way to create a log in, but that's what I've tried to do.
Again, I am very new to this, so any insight on where to look next would be greatly appreciated!
Thank you!
Do a view source and make sure your PHP code is actually being interpreted. If you see your PHP tags in the source of the browser then your web server is not configured properly.
Try
if(!empty( $_SESSION['email'] ))
and I hope you did session_start() at the beginning.
I think your *.php files does not behave like PHP files. You should check your web server settings.
You didn't use echo. Is it OK to try displying text without an echo statement before?
Related
I am implementing a simple newsletter form using PHP and CodeIgniter as part of a big project.
My HTML code is as follows.
<form method="POST" action='http://sit.com/index.php/Users/subscribenews'>
<div class="input-group">
<input type="email" id="pkemailid" name="pkemailid">
<span class="input-group-btn">
<button class="btn btn-lg btn-primary" type="submit"> Go
</button>
</span>
</div><!-- /input-group -->
</form>
In my Controller, I am handling the value as
$emailid = $this->input->post('pkemailid');
When I do a var_dump of the $_POST variable, I get the following
array(0) { }
The biggest problem is that I am able to run this correctly on my WAMP Server on localhost but it does not work on the hosted server.
Interestingly, if I change the POST to GET, then it starts working.
this line
<input type="email" id="pkemailid" name="pkemailid">
try changing it to
<input type="text" id="pkemailid" name="pkemailid">
type="email" is html 5 only and i don't know for certain -- but codeigniter might not support html 5 form values.
===== edit
so my suggestion when you are at the hair pulling questioning reality stage of trying to find a bug - get a fresh new copy of codeigniter, install on your server, make sure you get the welcome page.
now make the simplest form possible and echo something out from the controller method to make sure that you are submitting the form to the correct method. Next add a hidden form field with a value, and echo out that value from your method.
If all that works, then its not your server you have bug in your original code. And if it doesn't work then you have an issue with the server.
I am having an issue here, I'm working on my own website for university however I have came across a problem I can't find the answer to anywhere. I have a product page and a php module that handles adding the item to cart session data, however I'm getting this error:
https://saturn.csit.rmit.edu.au/~s3482230/wp/a2/product.php?productid=P001' was loaded over a secure connection, but contains a form which targets an insecure endpoint 'module: add to cart.php'. This endpoint should be made available over a secure connection. (the website is hosted on the school servers that you cant access without login on so you can't view it sadly.
but the form is writen as such...
<form class=productqty action="module: add to cart.php" method="post">
<p id=price>$<span id=pricedouble>0</span></p>
<input name="price" id=prodprice type="hidden" value="20.00" required/>
<input name="product code" type="hidden" value="P001" required/>
<input name="quantity" id=qtyinput maxlength="3" type="text" value="1"
onblur='checkButtons()' onkeypress='checkIfNumber(event)' required/>
<button id=negativebutton type="button" onclick="minusOne()" disabled> - </button>
<button id=positivebutton type="button" onclick="plusOne()"> + </button>
<input type="submit" value="Purchase" />
</form>
and the module code that handles adding to session and cart is: (unfinished)
<?php
session_start();
if (isset($_SESSION['user'])) {
}
$_SESSION['cart'][$_POST['product code']]['qty'] = $_POST['quantity'];
$_SESSION['cart'][$_POST['product code']]['price'] = $_POST['price'];
header("Location: ".$_SESSION["redirect_url"]);
?>
NOTE: the redirect_url is being used to redirect after logging on and off and works on those modules so it should work here, have also tried commenting the redirect out but doesn't change the warning, I hope this is enough information.
FIXED IT!
turns out, you can't use a lot of certain characters in the action="xxxx" method, I had to replace ':' with '%3A' and spaces with '%20'
so I used action="module%3A%20add%20to%20cart.php" and works fine.
I am trying to create the following condition
1) User visits page
2) User enters code
3) If the code is valid, the user enters a page corresponding to the code
4) If the code is not valid, the user fails
This is so that people can view certain PDFs for an apartment rentals company.
What I would like to do, is to create a form, with an input for the special code.
When the form is submitted, it will simply redirect to url.com/[[value of input]].php
I tried creating a PHP handler to do this, but when I try it out, I get a blank page. I have limited experience with PHP, so I might be off on a completely wrong path.
Thoughts greatly appreciated on this
Here's my form
<form id="enter_code" name="enter_code" action="code_handler.php" method="post">
<label for="code">Enter Your Apartment Code</label>
<input id="code" name="code" size="30" type="text" />
<input class="button" name="commit" style="font-weight:normal" type="submit" value="Sign in" />
</form>
and here's my code_handler.php
<?php
header( 'Location: http://www.opohills.com/guests/pdf/<?=htmlspecialchars($_POST['code'], ENT_COMPAT)?>.php' ) ;
?>
If you are inside PHP code you dont need to open the quotes again:
<?php
header( 'Location: http://www.opohills.com/guests/pdf/'.htmlspecialchars($_POST['code'], ENT_COMPAT).'.php' ) ;
?>
Why using php within php
header( 'Location: http://www.opohills.com/guests/pdf/'.htmlspecialchars($_POST['code'], ENT_COMPAT).'.php' ) ;
so I've been fighting with this for a few days, and I just can't seem to make it work. Whenever I press the submit button, the browser should send the post variables to write.php, but instead, it just redirects back to the website homepage, or the Document Root. This should be really, really simple, and I've done it before, but now it doesn't work for me. What I want to know is if this is a problem with my webserver setup, or PHP, or just a stupid mistake on my part. It's just a simple HTML form, not really special, so here's the form itself, in index.php:
<p style="font-size:13px">
<?php
$rp = fopen('mainlog.txt', 'r');
while(!feof($rp))
{
$read = fgets($rp);
echo($read).('<br/>');
}
fclose($rp);
?>
</p>
<form action="write.php" method="post">
Name: <input type="text" name="user" /><br/>
Changes:<br/>
<textarea cols="70" rows="8" name="change" style="background-color:#555;color:#ccc;font-family:verdana,arial,helvetica,sans-serif;font-size:13px"></textarea><br/>
<input type="submit" value="Add Entry"/>
</form>
And here's where it send to, in write.php:
<?php
$fp = fopen('mainlog.txt', 'a');
$wr1 = $_POST['change'];
#$my_t = getdate(date("g"));
date_default_timezone_set("America/New_York");
$date = date("n").('/').date("d").('/').date("Y").(', ').date("g").(':').date("i").(':').date("s");
$who = $_POST['user'];
$write = $date.(' by ').$who.('
').$wr1.('
');
fwrite($fp, $write);
fclose($fp);
header('Location: http://www.zennixstudios.com/first/chlog/');
?>
I have tried this both on my Apache 2.2 dedicated server with PHP 5 on FreeBSD8.2, and on XAMPP for Windows, with the same results. I have a suspicion that it may have something to do with PHP, specifically PHP include(), because I have several of those on this page, and when I put this on a friend's computer with XAMPP, but without the included files, the include()s just put errors on the screen, but the HTML form suddenly works fine. So, are there any known conflicts with HTML forms and certain PHP functions?
Other Notes:
The code shown above for index.php is within the main page div, but if you want the whole page source just ask.
I'm pretty sure the error isn't in write.php, because I KNOW that the browser never sends anything to it, because it would at least put the date in mainlog.txt.
If you want to see what this looks like in context, go to http://www.zennixstudios.com/first/chlog/
Thanks,
Chaos
Here is your problem:
<table align="right"><tr><td align="right"><form action="/" method="post">Username: <input action="login.php" type="text" name="uname"/><br/>Password: <input type="password" name="passwd"/><br/><input type="submit" value="Login" align="right"/></td></tr></table>
You never closed the form up in your header for the username and password, so your <form action="/" method="post"> is being used for pretty much the entire page and your write.php form action is being ignored because a form is already, technically, open. You'll need to close the form in your header for the rest of your page to work properly.
To reiterate: nothing is being redirected, you're actually posting all the data from both 'forms' to the location / as specified.
I have a php file that does a whole sleuth of user account stuff. It's purely being used to test if the features are working. However the script stops before the first feature is output. I have determined that the code is failing somewhere in this code:
...more above...
Authentication Form<br>
<br>
<?php if($this->session->userdata('authenticated')):?>
Welcome back <?php echo $this->session->userdata('userid');?><br>
<form action="<?php echo base_url();?>/account/logout" method="post">
<input type="submit" value="Log Out"/>
</form>
<?php else:?>
<form action="<?php echo base_url();?>/account/login" method="post">
Username: <input type="text" name="username"/><br>
Password: <input type="text" name="password"/><br>
<input type="submit" value="Login"/>
</form>
<?php endif;?><br>
...more below...
What i can not figure out is why this code if not executing. No errors are being outputted and i have used code similar to this before with no problem. Any help would be greatly appreciated.
One last note:
I'm using xampp running on my laptop and using codeigniter. newest of both.
The really snazzy way is to use xdebug http://www.xdebug.org/
The brute force way might be to lace your file with
echo __FILE__, ":", __LINE__, "\n<br/>";
and
var_dump(get_defined_vars());
plus also read up on setting error_reporting to an appropriate level for debugging plus show_errors in the PHP manual # php.net