Alert messages with php codeigniter 3 - php

I am trying to display a success message every time someone login/logout/create post/delete post etc.
At the beginning it didn't show on the screen and I changed a configuration in the session.php file that a lot of people suggested and the problem seemed fixed, but since then the messages don't go away even if I reload or go to another page.
This is the change I made in the session file
ini_set('session.id', $params['cookie_name']);
This is in the header of my page in the views folder
<?php if ($this->session->flashdata('user_registered')) : ?>
<?php echo '<p class="alert alert-dismissible alert-success">' . $this->session->flashdata('user_registered') . '</p>'; ?>
<?php endif; ?>
And this is in the controller
$this->session->set_flashdata('user_registered', 'You are now registered and can log in');

Related

Redirect user to another page if not logged in?

This may seem pretty confusing at first but I have a log in system on my website. I also have a forum on my website. What I want to do is make it so if people click on the forum button and they aren't logged in, it takes them to the login page, but if they are, it will take them to the forums. I have that in place but I am trying to do one more thing. I also want to make it so if people go in the url and type www.example.com/forums.php, it will check if they are logged in and if they aren't, take them back to the login page and if they are, proceed to take them there. I tried with this but it only works for the first part like I stated, not the rest.
<!-- Main Content -->
<p class="japanese">プレーンズ</p>
<p class="dev" contenteditable>currently under development</p>
<p class="clock"></p>
<p class="login">login</p>
<p class="register">register</p>
<?php
if (isset($_SESSION['u_username'])) {
echo '<p class="forums">forums</p>';
} else {
echo '<p class="forums">forums</p>';
}
?>
On top of your forums.php, check if the session is active, and if not, issue an HTTP redirect.
<?php
if (!isset($_SESSION['u_username'])) {
header('Location: login.php');
exit();
}
The exit is important to avoid running any more code on the page if the user is being redirected.
And the redirect should be placed before any output takes place, which means that it should go at the top of the file being executed.

yii flash message not displaying after redirection in view page

flash messages was working fine in local when i moved it into production(cpanel) its not displaying. I am not getting any error or warning.
In controller, i am setting flash like this
Yii::app()->user->setFlash('success', "Slider updated Successfully.");
and in view,
<?php if (Yii::app()->user->hasFlash('success')): ?>
<div class="success">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
The Yii flash messages are stored in the PHP session variables.
Are you able to set Session variables? Perhaps this will come in handy (Not able to set session variable in online server)

display session message only once during entire user session?

i have a html login form on my site that submits to login.php.
within login.php is a header redirect with a session message which echo's out onto the next page home.php.
what i am trying to do is make it so that this message only runs once and doesnt show again until the user logs in again. at the moment what is happening is the message is showing on each page refresh.
can someone please show me what i can do to sort this, thanks.
code in login.php:
<?php
if (logged_in())
{
$_SESSION['login_message']="<div class=\"login-overlay\">
<h1>Login You In Securely</h1>
</div>";
header("Location:home.php");
}
?>
code in home.php:
<?php session_start();
if(isset($_SESSION['login_message'] ))
echo $_SESSION['login_message'];
unset($_SESSION['loginframe2']) ;
?>
Simply unset the login message once it has been displayed.
if(isset($_SESSION['login_message'] )) {
echo $_SESSION['login_message'];
unset($_SESSION['login_message']);
}
Now if a user has seen the message, it won't be in the session anymore. And once he logs in again, login.php will set the variable again.
Just use and track a variable like $_SESSION['message_displayed']. Set it to true when you first display the message, and only display it if !array_key_exists('message_displayed', $_SESSION)

PHP - combining php with html to display logged in name

when the user logs in, their name gets saved into a session variable called 'name'. All pages should display the user's name if logged in, or a link to log in if the user is not logged in. I have tried the following code in the pages that require this feature, however I am just shown a white screen and I cant figure out where the error may be.
<?php if ( isset( $_SESSION['name'] )){ ?>
<p>Welcome back <?php echo $_SESSION['name'];?></p>
<?php} else{?>
Login <?php } ?>
EDIT: session has been started on the pages
Seems an erro at Line 3. You have missed spaces after open php tag
<?php } else{ ?>
^ ^
Have you added session_start() at start of your page?
This problem could be caused by not using session_start() before accessing $_SESSION[''].

Homepage not immediately recognizing session PHP

I have a site that displays two different versions of a navigation section depending on if a user is logged in or not.
<?php
if(isset($_SESSION['myusername'])){
echo 'Log Out';
}else{
echo 'Sign Up';
}
?>
The problem happens when a user is logged in and then closes the browser without logging out (and assuming they don't clear cache/cookies on browser exit).
When they open their browser later and come back to the site, the navigation displays as if they're not logged in. If they then click a link elsewhere on the site, i.e. My Account, the navigation then changes to show that they are logged in.
Any ideas what could be causing this? I'd like the navigation to show that they're logged in immediately upon coming back to the site.
First thing, check session_start() appears on your pages before any html, even the !DOCTYPE rule.
Now, on your index page add this:
<?php
session_start();
if(isset($_SESSION['username'])){
header("location: home.php"); // or whatever page you want your users to be redirected to...
}else {
?>
// here your html page should start
<html><head></head><body>
// all the DOM elements on your page
</body></html>
<?php
} // closing end of the else block started above
?>
Must be as below.
ob_start();
session_start();
//code to check session and other
ob_start() is for omitting header already sent error.

Categories