login with smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

login with smarty

Post by pleigh »

i wish to do a login system using smarty template engine, so far, i've used my previous version of login page, but cannot do it in smarty...

index2.tpl

Code: Select all

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
username: <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>">
password: <input type="password" name="password">
<input type="submit" name="submit" value="submit">
</form>

index2.php

Code: Select all

if (isset($_POST['submit']))
{
	$message = null;
	
	//username
	if (empty($_POST['username']))
	{
		$un = false;
		$message .= "please type your username";
	}
	else 
	{
		$un = $_POST['username'];		
	}
	
	//password
	if ($_POST['password'])
	{
		$pw = false;
		$message = 'plese type your password';
	}
	else 
	{
		$pw = $_POST['password'];
	}
	
	//process to username and password to db
	if ($un && $pw)
	{
		$query = "select user_id, username, password from users where username='$un' and password='$pw'";
		$result = @mysql_query($query) or die(mysql_error());		
		$row = mysql_fetch_array($result, MYSQL_NUM);
		
		if ($row)
		{
			// Start the session, register the values & redirect.
			session_start();
			$_SESSION['firstname'] = $row[1];
			$_SESSION['user_id'] = $row[0];
			header ("Location:  http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
			exit();
		}
		else 
		{
			$message .= "username and password do not match";
		}
		mysql_close();
	}
	$message = "please try again";
}

if (isset($message))
{
	echo "<font color=red>" . $message . "</font>";
}
i know using $smarty->assign() will assign some values from index2.php to index2.tpl, but i really don't know how to make this code work...please help...thanks in advance... :)

note: have deleted some smarty syntax for a fresh start
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: login with smarty

Post by jmut »

index2.tpl

Code: Select all

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
username: <input type="text" name="username" value="{$smarty.post.username|default:''}">
password: <input type="password" name="password">
<input type="submit" name="submit" value="submit">
</form>

index2.php

Code: Select all

if (isset($_POST['submit']))
...
	//password
	if (empty($_POST['password']))
	{
		$pw = false;
....
	if ($un && $pw)
	{
                $un = mysql_real_escape_string($un);
                $pw = mysql_real_escape_string($pw);
		$query = "select user_id, username, password from users where username='$un' and password='$pw'";
		....
Actually you don't need to assign anything as you don't show username.

If you want to show some data do like.

Code: Select all

$smarty->assign('my_smarty_var','fancy data');

and in the template use:

Code: Select all

{$my_smarty_var}   -> ouptut:: fancy data
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

thanks for the reply...i have an error
Access forbidden!

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

If you think this is a server error, please contact the webmaster.
Error 403
localhost
04/26/06 15:03:55
Apache/2.2.0 (Win32) DAV/2 mod_ssl/2.2.0 OpenSSL/0.9.8a mod_autoindex_color PHP/5.1.1
maybe from this code in the .tpl file

Code: Select all

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
and also, if i dont type anything in the username and password, the error message does not appear... :(
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

put this template in tmpl.html file in templates directory.

Code: Select all

<form action="" method="POST">
username: <input type="text" name="username" value="{$smarty.post.username|default:''}">
password: <input type="password" name="password">
<input type="submit" name="submit" value="submit">
</form>


in the php file do this at the end.


Code: Select all

$smarty->display('tmpl.html');

Try this out first
http://smarty.php.net/manual/en/install ... .basic.php
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

yup, did that...still same error... :(
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

pleigh wrote:yup, did that...still same error... :(
1. tell me dev env info - php version, apache etc. etc.
2. smarty version.
3. make simple simple script/template to make use of smarty.
4. set template_dir $smarty->template_dir = 'path/to/dir/where/templates/files/are'
5. turn off cacheing $smarty->caching = false;
6. turn smarty debugging on. $smarty->debugging = true;
7. try doing stuff one by one (regardin calls to smarty) -> figure out on which call does it crash?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

pleigh wrote:yup, did that...still same error... :(
btw looking at the error this probably has nothing to do with smarty.
are you running regular php scripts?
Post Reply