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
Related
$_SESSION Not working on CPANEL.
When we uploaded it to the web host using CPANEL the $_SESSION is not working.
But in local using XAMPP it is working.
you have make sure to write <?php session_start();?> in the first line of page
Sessions are enabled by default within any PHP installations and hence do not need any special configuration to enable sessions.
You should make sure you have session_start(); as the first line in any page where you want to use sessions; it should be the very first line, before any whitespace (an empty line, for example).
Hope it helps you.
you should insert session_start() in the beginning of the script.
Make confirm you call it on every page you need the access to session variables as well
Solved! the problem is, I put all my session on other php file so when include the php I put it under the html code.
I mean like this:
<html> <?php ?> </html>
so when I update it to this
<?php ?> <html>
all stuff are good. thanks for the comment guys <3
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.
As the title suggests, I am having problems trying to move an existing site over to a new host.
I have edited my .htaccess find to point the php.ini session.save_path value to a new folder stored in my non public root.
This is working fine, I can see the sessions appear in this folder, with the correct entries written to them.
But for some reason, my scripts cannot make use of these sessions, as in the variables associated to them hold no value, as in, they come out blank.
Now, these scripts are in use on my old host and do work perfectly. And comparing the actual session data, once the files have been downloaded off each host, they are both exactly the same.
This leads me to think that this could be a server side issue. Possibly another php.ini value.
Has this happened to anyone before, or can anyone suggest a reason behind this kind of behavior.
It anyone has absolutely any input regrading this, or could point me in the right direction as to solve this issue. It would be more than greatly appreciated.
Thank you!
#Marc
sess.php
<?php
session_start();
$_SESSION['test'] = 'test';
include 'sess2.php';
?>
sess2.php
<?php
echo ''.$test.'';
var_dump($test);
?>
session data file value
test|s:4:"test";
Now when I load sess.php it includes sess2.php but the page only displays the vardump which is NULL. This is odd because the data has been written to the session as shown in the downloaded data file value...
Any ideas?
Looks like you're depending on register_globals. That's a hideously BAD thing in PHP which defaults to off these days. Try echo $_SESSION['test'] instead now. As well, such variables are only registered at script startup time/session_start. You'd need to use session_register() (DON'T) to make it take effect during the current execution run
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 am making a simple Dynamic Website using PHP, where i allow the user to login and then access specific pages. So here's what i have done so far.
The logged in values are taken though $_POST variables in a php script where it fetches values from database for registered users. If the user is found i do the following:
session_register('userid');
$_SESSION['userid'] = $username;//this is taken from $_POST
$_SESSION['accesslevel'] = $access;
at the beginning of the php script i have put session_start();
Now here comes my problem.
At every page now i have to check if the user is allowed to view that page or not, if he ain't then he must be redirected to login.php, if he is then the page load must continue.
Now so far what i have learnt is that only way to maintain values across php pages is to use $_SESSION variables, and which ever page i am using Session Variables i must write session_start() on each page as the first line, else i will be getting Headers Already Sent error..
Strangely i exactly have done that but still get erros with the "headers already sent".
SO i want to what is the best way to design a website, where i have to use Session variables across most of the pages, and keep these common checks at a common place..
Can i use include() feature some how?
Are sessions only way to communicate data across php pages.
What is a better way?
I have the following code :
<?php
session_start();
if(!isset($_SESSION['user']))
{
$_SESSION['loc'] = "adminhome.php";
header("location:ettschoollogin.php");
exit();
}
?>
Which resides on top of every page which wants to check if the user has logged in.
And this is teh script to check for login
<?php
session_start();
include("connection.php");
$userid =$_POST['userid'];
$userpwd =$_POST['userpwd'];
$query="Select UNAME,UPASSWORD,SCHOOL,uaccess from schooluser where uname = '$userid'";
$result=mysql_query($query) or die("couldn't execute the query");
$row=mysql_fetch_array($result);
$useraccess = $row["uaccess"];
$school =$row[2];
if(($row[0]==$userid)&&($row[1]==$userpwd))
{
session_register('userid');
$_SESSION['userid']=$userid;
$_SESSION['school']=$school;
if($useraccess =="admin")
{
header("Location:adminhome.php");
}
if($useraccess !="admin")
{
header("Location:school_main.php");
}
}
else
{
header("Location:ettschoollogin.php?err=1");
}
?>
i was aware of the common error of having extra spaces after "?>", BUT I STILL GET IT.
Thanks guys, i missed out and the "connection.php" file actually had extra spaces after "?>" i had removed it before, but some how the file got rewritten again.Thanks a lot.
Yes, you can use include. Put all your common functions in a separate php file and "include" it at the top of each file.
You can use cookies to store information (typically just an id that you use to look up additional information in the PHP page). Normally, PHP sessions are handled using cookies though. See setcookie in the docs.
You are probably getting the error messages due to stray characters outside of a <?php ?> block. A common error is to have an extra blank line at the end of an include file, after the ?>. That blank line will be output and your headers will have been sent. If that isn't the problem, you will just need to make sure you move the session related code above any code that might generate some output (eg by using print or echo).
•Can i use include() feature some how?
Yes. You can do whatever you want before your session_start() call, only, you must not have outputted anything, not even a single space or character. Probably you have already outputted something, maybe on an automatic inclusion or apache prepend.
•Are sessions only way to communicate data across php pages.
•What is a better way?
Other ways are cookies, post and get parameters. But sessions are the only way to securely pass data among pages without sending them to the client and back (which may pose security risks)
Write ob_start(); at the top of your code and then you dont get the error of "headers already send"