views:

2505

answers:

5

I would like a batch file that can register a user into itself. something like:

@echo off
echo Choose an option:
echo     1:Register
echo     2:Login
Set option=
set /p option=Your option: 
if %option%==1 goto reg
if %option%==2 goto login
...
:reg
--The registration script--
goto login
...
:login
Set usr=
set /p usr=Username: 
if %usr%== goto pass
echo False username!
@pause
goto login
...
:pass
Set passw=
set /p passw=Password: 
if %passw%== goto success
echo False password!
@pause
goto login

So... something like that. 'if %passw%== "" and if %usr%== ""' === it should automatticly make that at registration.

It will be nice if more than 1 people could register.

A: 

Found some seriously evil tricks on this web site, including:

ECHO Enter some input, and press Enter when ready . . .
ECHO ←[13;0;64;13p
COPY CON USRINPUT.TMP
ECHO ←[13;13p
CLS
ECHO You typed:
TYPE USRINPUT.TMP

Which uses ANSI key translation to change Enter into CTRL-Z.

Andomar
yeah... great.. but not what im looking for.
YourComputerHelpZ
Getting an input string from a user is hard in a batch file; I couldn't image how hard setting up a registration system would be. Or how easily it could be broken; after all, everyone can read a batch file's source.
Andomar
This won't work correctly on any recent version of Windows. At least not the part with the ANSI escape sequences.
Joey
A: 

I couldn't quite say I fully understand your question, but if in doubts about anything related to batch scripting, I could recommend checking out this website this website.

The tutorial is really exhaustive, easy to follow and has helped me a number of times before.

Peter Perháč
it is the same as register for any website like Google Gmail, youtube... but than in a batch.
YourComputerHelpZ
+1  A: 

To test for an empty string, put quotes around both sides:

if "%usr%"==""

To append to a text file, use >>

echo %usr%:%pass% >> passwd.txt

To loop over the contents of a file, use for /f:

for /f "delims=: tokens=1,2" %%x in (passwd.txt) do (
  if "%%x"=="%usr%" then (
    set usrfound=true
  )
)
if "%usrfound%"=="" then (
  rem Register user
) else (
  rem Check password and succeed/fail
)

During testing or debugging, make sure echo is on. If you want to set and test a variable inside a for loop or other multiline statement, put this near the start of the file:

setlocal enabledelayedexpansion

and any time you need to read a variable that might have changed inside the block, use !var! instead of %var%.

JimG
Of course, the username and password file would have to be readable by the person running the batch file. So it would be trivially easy to open the file and get someone else's password. You could hash the passwords, although that would require a 3rd party executable. I don't think that anybody could (or should) write a SHA hash library in a batch file.
Kibbee
Good point, but it's also trivial to pass a password through a third party tool. For example:for /f "delims=" %%x in ('md5sum %pass%') do set hashpass=%%xHa, I just used 'trivial' to describe something done in a batch file. Writing an SHA hash is next on my list, just to see if it can be done (and then for an encore I will sandpaper my groin region).
JimG
A: 

Hmmm. This is what i found out by some help:

@echo off
title REGISTERANDLOGIN
if exist programbase.dll goto login
if not exist programbase.dll goto register

:register
title Register
echo Register
echo.
echo Please fill in the blanks.
echo.
Set usrname=
set /p usrname=Username: 
Set passw=
set /p passw=Password: 
echo %usrname%>> programdata.dll
echo %passw%>> programbase.dll
goto login

:login
:begin
set usr=
set /p usr=Enter your username: 
if {%usr%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (programdata.dll) do (
    if {%%a}=={%usr%} set authenticated=true
)

if not defined authenticated (echo Invalid Username & goto :begin)

:passwo
set pass=
set /p pass=Enter your password: 
if {%pass%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (programbase.dll) do (
    if {%%a}=={%pass%} set authenticated=true
)
if not defined authenticated (echo Invalid password & goto :begin)

:loggedin
cls
echo Welcome.
@pause
exit /b 0
YourComputerHelpZ
Calling it a .dll doesn't change that fact that it's storing passwords in plain text. This is good to secure something from your pet cat accidentally using it... nothing else.
Daniel Straight
YourComputerHelpZ
A: 
@echo off

if exist "%userprofile%\documents\Login\Accounts" goto start0

if not exist "%userprofile%\documents\Login\Accounts" md "%userprofile%\documents\Login\Accounts"

 :start0

title Login

color 02

cls

echo ---------------------------------

echo              Login.

echo ---------------------------------

echo.

echo.

echo.

echo 1.Create New Username.

echo 2.Log in.

echo.

echo.

echo.

set /p A=Choice:

IF '%A%' == '1' GOTO NewUser0

IF '%A%' == '2' GOTO Login0

IF NOT '%A%' == '1' GOTO start0

:NewUser0

cls

color 02

cd "%userprofile%\documents\Login\Accounts"

set /p newuser=New Username:

IF EXIST %newuser%.bat GOTO inval0

set /p newpass=New Password:

echo set realusername=%newuser% >> %newuser%.bat

echo set password=%newpass% >> %newuser%.bat

IF EXIST %newuser%.bat GOTO NewUser1

IF NOT EXIST %newuser%.bat GOTO NewUser2

:NewUser1

echo Your Account Has Been Created Successfully. Press An Key To Continue.

pause >nul

GOTO start0

:NewUser2

cls

color 0c

del /f /q %newuser%.bat

echo ERROR!Account Could Not Be Created For Unknown Reasons.Press Any Key To Continue

pause >nul

GOTO start0

:Login0

cls

color 02

cd "%userprofile%\documents\Login\Accounts"

echo Login.

echo.

echo.

echo.

echo.

set /p loguser=Username:

IF NOT EXIST %loguser%.bat GOTO inval1

:Login1

set /p logpass=Password:

call %loguser%.bat

if %logpass% == %password% goto success0

goto invalid2

:inval0

cls

color 0c 

echo Desired Username Already Exists. Press Any Key To Continue.

pause >nul

GOTO start0

:inval1

cls

color 0c 

echo Desired Username Does NOT Exist. Press Any Key To Continue.

pause >nul

GOTO start0

:invalid2

cls

color 0c 

echo Invalid Password. Press Any Key To Continue.

pause >nul

cls

color 02

echo Login.

echo.

echo.

echo.

echo.

echo Username:%loguser%

GOTO Login1

:Success0

cls

color 02

title End Of My Script

echo You Successfully Logged In.

echo Here Is Where You Continue The Script.

ping localhost -n 3 >nul

echo.

echo.

echo.

echo Press Any Key To Exit.

pause >nul 

exit