answersLogoWhite

0


Best Answer

Have a session_start(); at the very top of the page in php like this:

<?php

session_start();

?>

Then write a login form:

<form action="login.php" method = "POST">

<input type = "text" name = "username">

<input type = "password" name = "password">

<input type = "submit" value = "submit">

</form>

now start a new page called login.php. Put session_start(); at the top of the page again. Now you'll have two different $_POST variables from the form that got submitted. It's $_POST['username'] and $_POST['password']. the "name" of the input is what the $_POST variable is. And it's a $_POST because of the method of the form. Now you should change the names of the $_POST just to make it easier.

<?php

$username = $_POST['username'];

$password = $_POST['password'];

?>

Now it's time to see if that username and password are in the database

<?php

$mysql = "SELECT COUNT(*) FROM users WHERE username = '$username' AND password = '$password'"; // Select statement to find user

$query = mysql_query($mysql); // Query to see if user exists

$result = mysql_query($query); // Result for query. Should be either 1 or 0.

if ($result != 1)

{

?>

<script>

alert ("That username or password doesn't exist");

</script>

<meta http-equiv="refresh" content = "1; URL = index.php">

<?php // Javascript alert saying that username or password was wrong then loading the page back to the main page.

}

else

{

$_SESSION['username'] = $username; // Setting the variable for the username so now you can use $_SESSION['username'] anywhere on your website to display who is logged in.

}

?>

You have to put session_start(); at the top of every webpage though. Now for the logout. Create a page called logout.php. You can link to this page however you'd like. All you need for this page is:

<?php

session_destroy(); //destroys the current session

<meta http-equiv="refresh" content = "1; URL = index.php"> //Reloads the page back to the index.php

?>

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write login and logout code in php?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Login code for HTML?

If you mean how to create a login code with HTML, you need a tag called and a tag called and something in another programming language, such as php or java. There is no login code necessary to use HTML.


Where can one find a guide on how to make a login php script?

The best place to find a guide on how to make a login script in php is the PHP Easy Step website. They offer a step by step tutorial on how to create login scripts.


How do you write a PHP login for MsSQL to download a zip file?

See the link. This will display all zip related functions.


What is php code for online recruitment system?

write php coding for online carrier guidance and placement unit


How can you make your website have login and registration forms that work together?

This is usually done through PHP, which is a scripting language. If you are looking for a forum with login and registration, there are several programs around for free that you can use. Otherwise if you need something other than a forum, you will probably need to learn PHP which can take weeks to learn and years to master. I would suggest searching for PHP code or getting a book on learning PHP.


How do you make an account system in PHP?

If you would like to code this by yourself, you need to understand SQL language and php, and also (maybe) html. But, you can also find some scripts to download for free, like this one: free-php-login-script.com. It works great!


Can HTML tags code and text be included in a php code?

Yep. We do it all the time. One way is to simply tell PHP that you want the tags echoed. Like so: &lt;?php echo('&lt;b&gt;Hi&lt;/b&gt; Nice to meet you!'); ?&gt; Sometimes you want instead to simply use the PHP in parts of a page and leave the rest. You can do this, too. &lt;body&gt; &lt;div class="login"&gt; &lt;?php if($user-&gt;loggedIn()){ ?&gt; &lt;b&gt;Hi!&lt;/b&gt; &lt;?php }else{ ?&gt; &lt;!-- HTML FORM HERE --&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;/body&gt;


Write down a PHP code to add a session variable?

The preferred way is to simply alter the session superglobal. &lt;?php $_SESSION['variable'] = "value"; ?&gt;


How do you code a social network?

Most social networks are written in PHP. To write one, you would probably need to know HTML, CSS, and PHP. Once you are proficient those languages, you'll be able to write a social network.


What is php extension?

We can write php extension is many way... .php , .php5, .php4


How do you write a php algorithm?

Well if you know php then you can write your own algorithm php syntax is &lt;?php //open php tag $num = 1; $num2 = 2; $total = $num + $num2; echo $total; ?&gt; //close php tag


What is PHP enabled host?

A host that allows you to write in PHP